简体   繁体   中英

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 ?

.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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM