• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is not autowire a another type of dependendcy injection?

 
scott miles
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In most of the sites and books i have read two types os injection in spring that is

1)Constructor injection
2)Setter injection

As per my understanding autowiring is also a type of injection only. Is n't it? am i missing something here.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Autowiring is automatically doing your dependency injection for you through constructor or property/setter or even instance variable injection.

Autowiring just means that Spring will try to figure out what goes where, instead of you configuring all that yourself to tell Spring.

Mark
 
Kevin Cho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scott miles wrote:In most of the sites and books i have read two types os injection in spring that is

1)Constructor injection
2)Setter injection

As per my understanding autowiring is also a type of injection only. Is n't it? am i missing something here.


No, you're right. You're thinking too low level on how Spring does DI. It does it either though XML and Annotations. In XML, yes you do it through constructor or setter. In Annotations, you use @Autowired or other similar tags to wire other spring beans. So even at Annotation level you're either injecting through constructor or setter. Yes, you can use Annotation on constructor.

In annotation @Autowired through setter is like
@Autowired
private A a;

through constructor is like
public B(@Autowired a){
this.a = a;
}

Hope this make sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic