Spring inject without autowire annotation

kuba44 :

I find some answer: https://stackoverflow.com/a/21218921/2754014 about Dependency Injection. There isn't any annotation like @Autowired, @Inject or @Resource. let's assume that there isn't any XML configuration for this example TwoInjectionStyles bean (except simple <context:component-scan base-package="com.example" />.

Is it correct to inject without specify annotation?

Krzysztof Atłasik :

From Spring 4.3 annotations are not required for constructor injection.

public class MovieRecommender {

    private CustomerPreferenceDao customerPreferenceDao;

    private MovieCatalog movieCatalog;

    //@Autowired - no longer necessary
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }

    @Autowired 
    public setMovieCatalog(MovieCatalog movieCatalog) {
        this.movieCatalog = movieCatalog;
    }
}

But you still need @Autowired for setter injection. I checked moment ago with Spring Boot 1.5.7 (using Spring 4.3.11) and when I removed @Autowired then bean was not injected.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=440836&siteId=1