spring-in-action-5-samples icon indicating copy to clipboard operation
spring-in-action-5-samples copied to clipboard

still works without @Autowired

Open aCodeRancher opened this issue 6 years ago • 2 comments

Hi, I commented out all the @Autowired for all the JdbcTemplate in all JdbcXXXRepository and I also commented out the @Autowired for the constructor in DesignTacoController. But everything still works fine. I expected a null pointer exception. Why?

aCodeRancher avatar Jan 25 '19 19:01 aCodeRancher

For example in JdbcIngredientRepository, you can omit @Autowired, because there is an autowired by constructor.

lsdany avatar Jan 25 '19 19:01 lsdany

As of Spring Framework 4.3, an @Autowired annotation on such a constructor is no longer necessary if the target bean only defines one constructor to begin with. However, if several constructors are available, at least one must be annotated to teach the container which one to use.

Sammygracezhu avatar Aug 14 '19 16:08 Sammygracezhu