我遇到了一个我无法打败的问题。
我有一个控制器:
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
@Autowired
private HibernateUserDAO hibernateUserDAO;
//Here is some further code
这是我的HibernateDAO片段:
@Repository
@Transactional
public class HibernateUserDAO implements UserDAO {
@Autowired
private SessionFactory sessionFactory;
private Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
@Override
public void addUser(User user) {
getCurrentSession().save(user);
}
这是我的spring配置文件:
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="ua.macko"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources"/>
<tx:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/blog"/>
<property name="username" value="****"/>
<property name="password" value="****"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<ref bean="userFormatter"/>
</set>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>ua.macko.entity.User</value>
</list>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="format_sql">true</prop>
<prop key="show_sql">true</prop>
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
问题是我遇到了错误:
Could not autowire field: private ua.macko.dao.impl.hibernate.HibernateUserDAO ua.macko.controller.AdminController.hibernateUserDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ua.macko.dao.impl.hibernate.HibernateUserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
如果我从spring配置中删除tx:annotation-driven,我可以启动项目,但是当我尝试执行addUser()方法时,我遇到了错误:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
我该如何解决这个问题? tx:annotation如何驱动可以影响bean自动装配?
答案 0 :(得分:3)
如果将Transactional注释添加到类中,Spring将为该类创建一个代理来执行事务逻辑。如果bean实现了一个接口,Spring将创建一个基于JDK的代理,它实现了bean的所有接口。因此代理的类型在您的情况下UserDAO
而不是HibernateUserDAO
。
有两种解决方案:
1: 将自动装配字段的字段类型更改为接口:
@Autowired
private UserDAO hibernateUserDAO;
2:配置Spring以使用基于CGLIB的代理。这些代理扩展了bean类,因此它们与bean类本身具有相同的类型。如果您使用以下注释及其属性,则可以执行此操作:
@EnableTransactionManagement(proxyTargetClass = true)
或者,如果您对事务管理器使用XML配置:
<tx:annotation-driven proxy-target-class="true" transaction-manager="txManager"/>
您可以在Spring文档中阅读有关代理等的更多信息:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html#tx-decl-explained
答案 1 :(得分:2)
由于您使用的是@Transactional
,因此autoproxy生成器将实现The Interface。所以你需要自动连接到接口而不是实现。
@Autowired
private UserDAO hibernateUserDAO;