vipul12389
vipul12389

Reputation: 339

Spring3 @autowire Explain?

I have been going through the tutorials of Spring 3 at http://www.vaannila.com/spring/spring-annotation-controller-1.html

But i have one doubt regarding autowired feature of spring3.

If you look at sample code given at the site, it has used @Autowired before setUserService method. Now i have several question about it.

  1. Is this the standard of using autowire ?? that is create a interface, implement its method and then finally in your controller class use the interface class's setter method.

  2. if i use autowire in that way what is the benefit i got ?

  3. what is the substitute code of using autowire ?

Upvotes: 0

Views: 612

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340933

.1. Is this the standard of using autowire ?? that is create a interface, implement its method and then finally in your controller class use the interface class's setter method.

Yes, this is more-or-less standard way, but this is just a convention. Some consider this to be a bad practice. It is not enforced in any way by Spring (however you might be forced to add CGLIB to your dependencies if you don't use interfaces).

.2. if i use autowire in that way what is the benefit i got ?

Don't know what do you mean by that way, but just to name few benefits: easier testing, decoupling, less infrastructure code...

.3. what is the substitute code of using autowire ?

Again, hard to tell what do you mean by substitute code. There are plenty of other techniques: you can use XML, @Configuration, @Resource, @Inject, (probably few other ats), constructor injection, field injection, setter injection (the one in the tutorial), looking up Spring beans directly from BeanFactory...

Upvotes: 1

Related Questions