2014-02-15 111 views
2

我想創建例如從用的Apache Tomcat和PostgreSQL Spring的事務而獲得自動裝配依賴的錯誤注入失敗自動佈線依賴項的注入失敗;嵌套的例外是org.springframework.beans.factory.BeanCreationException:

異常

javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception 
    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:724) 

root cause 

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private DAO.TblUserManager_DAO Controllers.LoginController.user; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [DAO.TblUserManager_DAO] is defined: expected single matching bean but found 2: [tblUserManager_DAOImpl, TblUserManager_DAOImpl] 
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) 
    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:294) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
    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:631) 
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
    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:724) 

root cause 

org.springframework.beans.factory.BeanCreationException: Could not autowire field: private DAO.TblUserManager_DAO Controllers.LoginController.user; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [DAO.TblUserManager_DAO] is defined: expected single matching bean but found 2: [tblUserManager_DAOImpl, TblUserManager_DAOImpl] 
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506) 
    org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) 
    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:294) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
    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:631) 
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
    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:724) 

root cause 

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [DAO.TblUserManager_DAO] is defined: expected single matching bean but found 2: [tblUserManager_DAOImpl, TblUserManager_DAOImpl] 
    org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800) 
    org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) 
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) 
    org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) 
    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:294) 
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
    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:631) 
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
    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:724) 

我dispacher-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

    <context:annotation-config /> 
    <context:component-scan base-package="Controllers" /> 
    <context:component-scan base-package="DAO" /> 
    <mvc:annotation-driven /> 
    <tx:annotation-driven transaction-manager="transactionManager"/> 


    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" 
      p:suffix=".jsp" /> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql://localhost:5432/car" /> 
     <property name="username" value="----" /> 
     <property name="password" value="-----" /> 
    </bean> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="annotatedClasses" > 
      <list> 
       <value>DAOModel.Tblusers</value> 
       <value>DAOModel.Tblcars</value> 
      </list> 
     </property> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="DAOModel" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
     <!-- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> --> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" > 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
    <bean id = "Tblusers_DAOImpl" class="DAO.Tblusers_DAOImpl"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
    <bean id = "TblUserManager_DAOImpl" class="DAO.TblUserManager_DAOImpl"> 

    </bean> 
</beans> 

我的倉庫,服務組件

@Component 
    public interface Tblusers_DAO { 
     public void adduser(Tblusers user); 
    } 


    @Repository 
    public class Tblusers_DAOImpl implements Tblusers_DAO{ 

     @Autowired 
     private SessionFactory sessionFactory; 

     @Override 
     public void adduser(Tblusers user) { 
      this.getSessionFactory().getCurrentSession().saveOrUpdate(user); 
     } 


     public SessionFactory getSessionFactory() { 
      return sessionFactory; 
     } 


     public void setSessionFactory(SessionFactory sessionFactory) { 
      this.sessionFactory = sessionFactory; 
     } 
    } 



@Component 
public interface TblUserManager_DAO { 
    public void adduser(Tblusers user); 
} 



@Service 
public class TblUserManager_DAOImpl implements TblUserManager_DAO{ 

    @Autowired 
    private TblUserManager_DAO dao; 

    @Override 
    @Transactional 
    public void adduser(Tblusers user) { 
     dao.adduser(user); 
    } 

    public void setDao(TblUserManager_DAO dao) { 
     this.dao = dao; 
    } 

} 

和控制器

public class LoginController { 

    @Autowired 
    private TblUserManager_DAO user; 
    @RequestMapping(value = "/Login", method = RequestMethod.POST) 
    public ModelAndView setLgin(@ModelAttribute("LoginModel") LoginModel login) { 
     ModelAndView model = new ModelAndView(); 
     Tblusers user2=new Tblusers(); 
     user2.setTblusersId(3); 
     user2.setTblusersFamily(login.getUsername()); 
     user2.setTblusersPassword(login.getPassword()); 
     user.adduser(user2); 
     model.addObject("Hello", login.getUsername()); 
     return model; 
    } 
public void setUser(TblUserManager_DAO user) { 
     this.user = user; 
    } 

} 
+0

[Spring自動裝配中的BeanCreationException]的可能重複(http://stackoverflow.com/questions/9408721/beancreationexception-in-spring-autowiring) –

回答

3

當組件掃描類與@Component@Repository@Service@Configuration等註釋,春隱式創建爲它的bean。當你顯式聲明一個bean時,它也爲它創建一個bean,即。這

<bean id = "Tblusers_DAOImpl" class="DAO.Tblusers_DAOImpl"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 
<bean id = "TblUserManager_DAOImpl" class="DAO.TblUserManager_DAOImpl"> 

所以現在,你有兩個豆TblUserManager_DAOImplTblusers_DAOImpl。春天不知道哪一個注入像

@Autowired 
private TblUserManager_DAO user; 

因爲有兩個候選人。要麼刪除<bean>聲明,要麼使用類似@Qualified的資格來限定注入目標,以選擇合適的bean。

Here's the documentation.通過它來了解組件掃描的全過程。


由於JB Nizet stated below,你應該只用@Component或任何其專業化的註釋實際的實現類。從語義上考慮,接口不是組件,不能爲接口創建bean。只有實際的類,接口的實現可以是一個組件。

+1

+1。並且不要使用@Component註釋bean的接口。只有實現應該註釋。 –

+0

當我在Controller中添加@Qualifier(「user」)時,此解決方案不起作用。 – ATJVS

+0

@ATJVS向我解釋爲什麼你會用'用戶'值來限定它。你爲什麼認爲這應該起作用? –

2

無論是通過xml文件beandefenition條目,或通過像@零部件,@庫,@ Service.Moreover註解註冊您的豆類,

<context:annotation-config/> and <context:component-scan/> 

這些元素無論是在你的code.You使用可以簡單地使用<context:component-scan/>適當,因爲它會做與其他同樣的事情,也爲了避免模糊

0

無論何時注入任何對象,您都必須提供setter和getter方法。它會解決你的問題。

相關問題