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.
-
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.
-
if i use autowire in that way what is the benefit i got ?
-
what is the substitute code of using autowire ?
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).
Don’t know what do you mean by that way, but just to name few benefits: easier testing, decoupling, less infrastructure code…
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 fromBeanFactory…