[英]Transaction Manager fails in Spring+Hibernate configuration
i am getting an error like below when i set up a transaction manager in spring, if i don't use it in spring, all is working fine 我在春季设置事务管理器时遇到如下错误,如果我在春季不使用它,则一切正常
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 is like below 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 class is like below 服务等级如下
@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);
}
Everything works fine if i do not use Transaction Manager, 如果我不使用交易管理器,一切都会正常,
please advise what i might have missed here 请告知我在这里可能错过的地方
Strange behavior. 奇怪的行为。 The DepartmentManager
interface should not be a problem. DepartmentManager
界面应该没有问题。
Just a guesswork : Is the problem doesn't come that you declare twice the transaction manager : 只是一个猜测:问题是不会出现两次声明事务管理器的问题:
<tx:annotation-driven transaction-manager="transactionManager"/>
and 和
<tx:annotation-driven/>
Maybe it creates a side-effect. 也许会产生副作用。 You could try by removing one of them and by keeping the DepartmentManager
interface. 您可以尝试删除其中之一并保留DepartmentManager
界面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.