2011-08-07 51 views
16

我有一個實現相同的接口

ServiceClass1

@Service 
public class ServiceClass1 implements ItemsService{ 

@Autowired 
Class1DaoImpl class1Dao; 

@Transactional 
public List<Class1> getAllItems() { 
    return class1Dao.getAllItems(); 

} 

ServiceClass2

@Service 
public class ServiceClass2 implements ItemsService{ 

@Autowired 
Class2DaoImpl class2Dao; 

@Transactional 
public List<Class2> getAllItems() { 
    return class2Dao.getAllItems(); 

} 

兩個服務類這是控制器類

 @Controller 
    public class IndexController { 


    @Autowired 
    ServiceClass1 serviceClass1; 

    @Autowired 
    ServiceClass1 serviceClass2;  

     ............ 
     ..... 
} 

我得到這個錯誤時,被請求的頁面

java.lang.IllegalArgumentException: Can not set com.package.ServiceClass1 field com.package.IndexController.serviceClass1 to $Proxy25 
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146) 
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150) 
sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63) 
java.lang.reflect.Field.set(Field.java:657) 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:500) 
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) 
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:609) 
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:571) 
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:623) 
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:491) 
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:432) 
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) 
javax.servlet.GenericServlet.init(GenericServlet.java:212) 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
java.lang.Thread.run(Thread.java:662) 

我做了一些谷歌,荷蘭國際集團,似乎無法找到任何堅實的答案,爲什麼我收到此錯誤?

回答

41

您需要對自動裝配接口類型,而不是類的類型,即

@Autowired 
ItemsService serviceClass1; 

如果你有實現ItemsService多個Bean,那麼自動裝配是不是真的合適。考慮使用@Resource代替:

@Resource(name="serviceClass1") 
ItemsService serviceClass1; 

@Resource(name="serviceClass2") 
ItemsService serviceClass1; 
+0

@skaffman ..所以我在哪裏指定資源bean名稱?我的意思是我沒有在xml文件中聲明服務bean,或者至少我認爲沒有必要這樣做,因爲我使用「component-scan」..沒有在bean配置文件中顯式聲明,名稱「serviceClass1 「和」serviceClass2「已解決? – RKodakandla

+0

@rrkwells:bean的名字是從類名中推斷出來的(例如'com.x.y.ServiceClass1' =>'serviceClass1'。 – skaffman

+2

我真的不喜歡這種方法 - 如果類名改變怎麼辦? – Dejell