事务管理器在Spring + Hibernate配置中失败

时间:2016-08-20 13:51:51

标签: java spring hibernate service dao

我在春天设置了一个事务管理器时遇到如下错误, 如果我不在春天使用它,一切都运转良好

Aug 20, 2016 3:45:53 PM org.springframework.orm.hibernate4.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@3148f668] of Hibernate SessionFactory for HibernateTransactionManager
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.local.service.DepartmentManagerImpl] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:319)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)

Spring.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="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-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.local.service"/>
    <context:annotation-config/>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean class="com.local.implement.ImplDepartment" id="implDepartment">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean class="com.local.implement.ImplEmployee" id="implEmployee">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>


    <bean class="com.local.service.EmployeeManagerImpl" id="employeeManagerImpl">
        <property name="employeeDAO" ref="implEmployee"/>
    </bean>

    <bean class="com.local.service.DepartmentManagerImpl" id="departmentManagerImpl">
        <property name="departmentDAO" ref="implDepartment"/>
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.local.logic"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
            </value>
        </property>

    </bean>


    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/hiber"/>
        <property name="username" value="root"/>
        <property name="password" value="Pemadmin123!"/>
    </bean>


    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

</beans>

服务类如下所示

@Service
    public class DepartmentManagerImpl implements DepartmentManager {

        private DepartmentDAO departmentDAO;

        @Override
        @Transactional
        public List<Department> getAllDepartments() {

            return departmentDAO.getAllDepartments();

        }

        @Autowired
        public void setDepartmentDAO(DepartmentDAO departmentDAO) {
            this.departmentDAO = departmentDAO;
        }
.....
}



@Repository
public class ImplDepartment implements DepartmentDAO {

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

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

public interface DepartmentDAO {

    public void addDepartment(Department department);

    public void editDepartment(Department department);

    public void deleteDepartment(Integer department);

    public Department getDepartmentById(Integer department_id);

    public List<Department> getAllDepartments();

    public void addEmployee(Department department, Employee employee);

}


public interface DepartmentManager {

    public void addDepartment(Department department);

    public void editDepartment(Department department);

    public void deleteDepartment(Integer department);

    public Department getDepartmentById(Integer department_id);

    public List<Department> getAllDepartments();

    public void addEmployee(Department department, Employee employee);

}

如果我不使用事务管理器,那么一切正常,

请告知我可能错过的地方

1 个答案:

答案 0 :(得分:0)

奇怪的行为。 DepartmentManager接口应该不是问题。
只是一个猜测:问题是不是你宣布两次 交易经理:

<tx:annotation-driven transaction-manager="transactionManager"/>

<tx:annotation-driven/>

可能会产生副作用。 您可以尝试删除其中一个并保留DepartmentManager界面。