viernes, 31 de enero de 2014

Priorities of dependencies resolution in Spring framework

When there are both implementations of a concrete service interface, one defined using annotation and another one using spring configuration. Which implementation will Spring use to resolve that interface?

The application can be downloaded here
Spring api for this application:
  • spring-context 4.0.1.RELEASE
  • spring-webmvc 4.0.1.RELEASE
  • spring-web 4.0.1.RELEASE
Let's start:
  • In Java-Spring-LAB/src/main/com/java/spring/lab/controllers/ServiceController.java, there is a field using an autowired annotation to resolve the Service implementation.
  • In Java-Spring-LAB/src/main/com/java/spring/lab/services/Service.java is the interface to resolve.
  • In Java-Spring-LAB/src/main/com/java/spring/lab/services/impl/NameService.java is the implementation that is resolved by autoscan components (by including the Service annotation).
  • In Java-Spring-LAB/src/main/resources/spring/mvc-config.xml, there is a "service" bean with MyNameService reference implementation which is in Java-Spring-LAB/src/main/com/java/spring/lab/services/impl/MyNameService.java.
In the current status, with the commented bean in mvc-config.xml, the NameService class is properly resolved due to autoscan components tag is set in such spring properties.
When user uncomments the bean of MyNameService (important to specify the id), the MyNameService class is used insteads of NameService class.
Therefore, the XML settings prioritizes its configuration againts annotations.

No hay comentarios:

Publicar un comentario