当我运行tomcat时,我得到NoSuchBeanCreation错误

时间:2015-11-11 04:43:41

标签: java spring hibernate spring-mvc

我想使用spring和hibernate做小应用程序,我遇到了上述问题,我已经查看了与上述问题相关的答案,但我无法使用这些答案解决它。

包结构

  1. com.library.bean
  2. com.library.controller
  3. com.library.dao
  4. com.library.service
  5. 每个包下的文件都是

      

    com.library.bean - LoginBean.java

    中      

    com.library.controller 中 - LoginController.java

         

    com.library.dao - DataAccessObject.java

         

    com.library.service - UserCreditentialsService.java

    很抱歉发布长代码

    LoginController.java

    package com.library.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.library.bean.LoginBean;
    import com.library.service.UserCreditentialsService;
    
    @Controller
    public class LoginController {
    
        @Autowired
        private UserCreditentialsService service;
    
        public void setService(UserCreditentialsService service) {
            this.service = service;
        }
    
        @RequestMapping(value="/",method=RequestMethod.GET)
        public void renderPageService(){
            String username="james";
            String password="johnson";
            LoginBean bean = new LoginBean();
            bean.setUsername(username);
            bean.setPassword(password);
            this.service.checkValidUser(bean);  
        }
    
    }
    

    DataAccessObject.java

    package com.library.dao;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    import com.library.bean.LoginBean;
    
    @Repository
    public class DataAccessObject {
    
        @Autowired
        private SessionFactory sessionFactory;
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
        public boolean isValidUser(LoginBean bean){
            Session session = this.sessionFactory.getCurrentSession();
            session.save(bean);
            return true;
        }
    
    
    }
    

    UserCreditentialsService.java

    package com.library.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.library.bean.LoginBean;
    import com.library.dao.DataAccessObject;
    
    @Service
    public class UserCreditentialsService {
    
        @Autowired
        private DataAccessObject dao;
    
        public void setDao(DataAccessObject dao) {
            this.dao = dao;
        }
    
        @Transactional
        public boolean checkValidUser(LoginBean bean){
            return this.dao.isValidUser(bean);
        }
    
    }
    

    LoginBean.java

            package com.library.bean;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    import org.springframework.stereotype.Component;
    
    @Component
    @Entity
    @Table(name="Login")
    public class LoginBean {
    
        @Id
        @Column(name="username")
        private String username;
    
        @Column(name="password")
        private String password;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    弹簧servlet.xml中

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <annotation-driven />
    
    
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/jsp/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
    
    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <beans:property name="url"
            value="jdbc:oracle:thin:@localhost:1521:xe" />
        <beans:property name="username" value="hr" />
        <beans:property name="password" value="hr" />
    </beans:bean>
    
    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.library.bean.LoginBean</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>
    
    <beans:bean id="logindao" class="com.library.dao.DataAccessObject">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>
    
    
    
    <beans:bean id="service" class="com.library.service.UserCreditentialsService">
        <beans:property name="dao" ref="logindao"/>
    </beans:bean>
    
     <context:component-scan base-package="com.library" />
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean> 
    

    的web.xml

    <display-name>Library</display-name>
        <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
      

    我得到的错误是

         
        

    org.springframework.beans.factory.BeanCreationException:创建名为'service'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.library.dao.DataAccessObject com.library.service.UserCreditentialsService.dao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义[com.library.dao.DataAccessObject]类型的唯一bean:期望的单个匹配bean但找到2:[logindao,dataAccessObject]

      

1 个答案:

答案 0 :(得分:0)

问题是您有2个DataAccessObject类型的bean:logindao和dataAccessDao。

因此,Spring无法决定您希望在UserCredentialsService中使用哪一个并拒绝启动。

打破这两个bean的层次结构,这个错误消失了。