habuma / spring-in-action-5-samples

Home for example code from Spring in Action 5.
Apache License 2.0
1.21k stars 1.04k forks source link

still works without @Autowired #38

Open aCodeRancher opened 5 years ago

aCodeRancher commented 5 years ago

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?

lsdany commented 5 years ago

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

Sammygracezhu commented 5 years ago

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.