簡體   English   中英

事務管理器在Spring + Hibernate配置中失敗

[英]Transaction Manager fails in Spring+Hibernate configuration

我在春季設置事務管理器時遇到如下錯誤,如果我在春季不使用它,則一切正常

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);

}

如果我不使用交易管理器,一切都會正常,

請告知我在這里可能錯過的地方

奇怪的行為。 DepartmentManager界面應該沒有問題。
只是一個猜測:問題是不會出現兩次聲明事務管理器的問題:

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

<tx:annotation-driven/>

也許會產生副作用。 您可以嘗試刪除其中之一並保留DepartmentManager界面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM