0
votes

I am using Spring MVC annotation driven configuration. I have a repository class named ProductRepository annotated with @Repository. In my ProductController class I have a private field ProductRepository productRepository annotated with @Autowired.

  1. Is the following correct? I want to verify if my precise understanding is correct? Is there any other technical detail that would be a good supplement to this? Now when the web app is initialized a bean is created for each of the annotated classes in <context:component-scan>. During request processing when I access productRepository a call to Spring libraries finds a bean of type ProductRepository and then returns a reference to it.
  2. May be the answer to this question is predicated on the answer to the previous one. What happens when I have a IProductRepository interface and I autowire an instance to this interface. If I have two classes implementing this interface, then how will autowiring find the right bean? Does autowiring work with Interfaces?

I am asking the second question because currently I am autowiring instances of Repository bean to my Controller class. As a good design practice I found that I should really use a DAO. My understanding is that the DAO (an interface) will be autowired in this case and there can be multiple implementations for this DAO (Hibernate, JDBC). Now if I have multiple Repository classes implementing this DAo interface and my Controller has an autowired field for the DAO interface how will it inject the appropriate DAO implementation?

Do I have to resort to XML configuration? What are my options for Dependency Injection here?

Update:

java.lang.NullPointerException
    at repository.ProductRepository.findProductByCollectionNumber(ProductRepository.java:179)
    at repository.ProductRepository$$FastClassByCGLIB$$ecfba3d5.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
    at repository.ProductRepository$$EnhancerByCGLIB$$b45a8183.findProductByCollectionNumber(<generated>)
    at controller.ProductController.findProductPagination(ProductController.java:198)
1

1 Answers

0
votes

When Autowiring by type, if more than one bean is found the bean factory rise a Exception. You can solve it using @Qualifier annotation, see http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers

or switching to Autowiring by name with @Resource annotation.