【发布时间】:2016-08-20 13:51:51
【问题描述】:
我在春季设置事务管理器时遇到如下错误, 如果我在春天不使用它,一切都很好
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 被注释为 @Repository 对吗?你也可以尝试注解私有的DepartmentDAO,departmentDAO;使用自动连线@。但我需要编写更多代码。
-
在描述中添加更多代码......所有依赖项都已正确设置并且一切正常,除非我添加事务管理器
-
wooohoo,通过删除 DepartmentManager 界面设法解决了问题,您能帮我理解为什么它会导致问题吗?
标签: java spring hibernate service dao