简体   繁体   中英

Some doubts about the use of **@Autowired** annotation and interface declaration in Spring Framework

I am quite new in Spring framework and I have some questions about the use of @Autowired annotation and interface declaration.

Referring to this example:

http://viralpatel.net/blogs/spring3-mvc-hibernate-maven-tutorial-eclipse-example/

I know that @Autowired annotation can be used to automatically link a bean on a property.

In the previous example I have the following situation:

I have a ContactDAO interface and it's implementation class named ContactDAOImpl

Next in the class ContactServiceImpl there is this variable annoted using @Autowired :

@Autowired
private ContactDAO contactDAO;

My first doubt is related to the fact that ContactDAO is an interface so what am I wiring? The concrete type: ContactDAOImpl ? If yes, is the Spring Framework that do it?

The second doubt is related to the fact that in the spring-servlet.xml configuration file there is not a bean definizion for the ContactDAO orf ContactAOImpl class...why? Is it because ContactDAOImpl class is annoted using @Repository annotation?

Thanks

Andrea

My first doubt is related to the fact that ContactDAO is an interface so what am I wiring? The concrete type: ContactDAOImpl ? If yes, is the Spring Framework that do it?

Spring will autowire an implementation of the interface for you, as long as there's only one matching implementation. There's also a way to match a single implementation from multiple candidates to your autowiring by using @Qualifier with @Autowired and naming the implementation.

The second doubt is related to the fact that in the spring-servlet.xml configuration file there is not a bean definizion for the ContactDAO orf ContactAOImpl class...why? Is it because ContactDAOImpl class is annoted using @Repository annotation?

If you're using annotations ( @Component , @Repository , @Service , @Controller ) in your implementations for configuration, you don't need to explicitly define the bean in the xml (although you can do that also).

Edit: this old answer of mine might shed some more light about using annotations in Spring.

The answers to your two questions are Yes, and Yes.

In fact, you might not have an instance of ContactDAOImpl autowired in the service, but an instance of a proxy, which deletages to an instance of ContactDAOImpl. The proxy will typically handle transactions, translate exceptions, etc.

And the @Repository annotation is an alternative (simpler) way to declare a Spring bean. It works only if you have an element in the Spring xml file telling it to discover annotated beans.

Spring will autoscan all your classes and find all annotated classes and register them, this in your spring config will tell it to do that:

<context:component-scan base-package="my.base.package" />

Therefore you do not need to declare your @Repository in your configuration file.

Onto the first part of your question, this is the unpinning of the IOC pattern ; your Service class is only aware of the interface of the DAO, this means that it is not dependent on the implementation.

During scanning Spring will find all of your annotated classes and when you ask for an @Autowired then it will attempt to find a class that your have annotated that is an implentor of the interface you have asked to have Autowired.

Have a look at the Spring documentation on Annotation Configuration .

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