How does spring mvc annotation driven link a repository object

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)

Answers


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.


Need Your Help

Javascript to Measure Multiple Div Heights

javascript jquery css height measurement

I'm trying to build out an accordion menu with jQuery but am running into jumpy animations. The jumpiness is caused by not setting a height for the div. My problem is that each div is going to ha...

How to make derived textbox and toolstriptextbox use same code in Vs2010

visual-studio-2010 textbox inheritance

I've created a derived WinForm textbox control that knows how to provide a hint (those grayed out words that say things like "Password" or "Enter search here") as well as allowing for the beep on e...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.