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

annotations, autowired, java, spring, spring-mvc

Solution

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.

Problem

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

Original source

Related problems