dependency injection - How does spring mvc annotation driven link a repository object -


i using spring mvc annotation driven configuration. have repository class named productrepository annotated @repository. in productcontroller class have private field productrepository productrepository annotated @autowired.

  1. is following correct? want verify if precise understanding correct? there other technical detail supplement this? when web app initialized bean created each of annotated classes in <context:component-scan>. during request processing when access productrepository call spring libraries finds bean of type productrepository , returns reference it.
  2. may answer question predicated on answer previous one. happens when have iproductrepository interface , autowire instance interface. if have 2 classes implementing interface, how autowiring find right bean? autowiring work interfaces?

i asking second question because autowiring instances of repository bean controller class. design practice found should use dao. understanding dao (an interface) autowired in case , there can multiple implementations dao (hibernate, jdbc). if have multiple repository classes implementing dao interface , controller has autowired field dao interface how inject appropriate dao implementation?

do have resort xml configuration? options dependency injection here?

update:

java.lang.nullpointerexception     @ repository.productrepository.findproductbycollectionnumber(productrepository.java:179)     @ repository.productrepository$$fastclassbycglib$$ecfba3d5.invoke(<generated>)     @ net.sf.cglib.proxy.methodproxy.invoke(methodproxy.java:204)     @ org.springframework.aop.framework.cglib2aopproxy$cglibmethodinvocation.invokejoinpoint(cglib2aopproxy.java:689)     @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:150)     @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:110)     @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172)     @ org.springframework.aop.framework.cglib2aopproxy$dynamicadvisedinterceptor.intercept(cglib2aopproxy.java:622)     @ repository.productrepository$$enhancerbycglib$$b45a8183.findproductbycollectionnumber(<generated>)     @ controller.productcontroller.findproductpagination(productcontroller.java:198) 

when autowiring type, if more 1 bean found bean factory rise exception. can solve using @qualifier annotation, see http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers

or switching autowiring name @resource annotation.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -