Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring inject without autowire annotation

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?

like image 828
kuba44 Avatar asked Oct 02 '17 09:10

kuba44


People also ask

Is @autowired required for constructor injection?

Here the quote from the official documentation : 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.

Is Autowired annotation mandatory?

Is @Autowired annotation mandatory for a constructor? No. After Spring 4.3 If your class has only single constructor then there is no need to put @Autowired .

What can I use instead of Autowired?

You can indicate a @Primary candidate for @Autowired. This sets a default class to be wired. Some other alternatives are to use @Resource, @Qualifier or @Inject.

What is the difference between @autowired and @inject annotation in spring?

@Inject and @Autowired both annotations are used for autowiring in your application. @Inject annotation is part of Java CDI which was introduced in Java 6, whereas @Autowire annotation is part of spring framework. Both annotations fulfill same purpose therefore, anything of these we can use in our application.


2 Answers

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 a moment ago with Spring Boot 1.5.7 (using Spring 4.3.11) and when I removed @Autowired then bean was not injected.

like image 114
Krzysztof Atłasik Avatar answered Oct 14 '22 22:10

Krzysztof Atłasik


Yes, example is correct (starting from Spring 4.3 release). According to the documentation (this for ex), if a bean has single constructor, @Autowired annotation can be omitted.

But there are several nuances:

1. When single constructor is present and setter is marked with @Autowired annotation, than both constructor & setter injection will be performed one after another:

@Component
public class TwoInjectionStyles {
    private Foo foo;

    public TwoInjectionStyles(Foo f) {
        this.foo = f; //Called firstly
    }

    @Autowired
    public void setFoo(Foo f) { 
        this.foo = f; //Called secondly
    }
}

2. At the other hand, if there is no @Autowire at all (as in your example), than f object will be injected once via constructor, and setter can be used in it's common way without any injections.

like image 37
Alex Saunin Avatar answered Oct 14 '22 23:10

Alex Saunin