Reputation: 213
I am using a SpringBoot
application. I am trying to inject a service object in the controller class.I didn't use @Autowired
above the object and constructor but still the object got injected.
public class Test {
private Test1 test1;
public Test(Test1 test1) {
this.test1 = test1;
}
}
In the above example, test1
got injected even though I didn't use @autowire
d anywhere.
Could someone explain it?
Upvotes: 1
Views: 311
Reputation: 15844
Starting with Spring 4.3
,If you have a single constructor
with dependencies as constructor parameter in your class then spring will automatically inject it for you.
As per Spring @Autowired docs
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.
public class Test {
private Test1 test1;
public Test(Test1 test1) {
this.test1 = test1;
}
}
Upvotes: 3