java - Spring+HibernateDAO DAO bean 未找到

标签 java spring hibernate spring-mvc

我遇到了无法克服的问题。

我有一个 Controller :

@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:driven 如何影响 bean Autowiring ?

最佳答案

如果您将 Transactional 注解添加到您的类中,Spring 将为该类创建一个代理来执行事务逻辑。如果bean实现了一个接口(interface),Spring将创建一个基于JDK的代理,它实现了bean的所有接口(interface)。因此,在您的情况下,代理的类型是 UserDAO 而不是 HibernateUserDAO

有两种解决方案:

1: 将 Autowiring 字段的字段类型更改为接口(interface):

@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

关于java - Spring+HibernateDAO DAO bean 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262693/

相关文章:

java - JFreeChart 未出现在 JPanel 上 - 可能与我的代码逻辑有关

java - hibernate 生成错误的查询

spring - Spring Security可以解析 header 并验证身份验证信息吗?

java - 当两个事务同时发生并相互影响时,根据另一个表的数据更新一行表的方法是什么

java - 向上导航文件夹?

Java手动堆栈分配

spring - Liquibase includeAll 标签被忽略

java - 从 Quartz 作业查询数据库时出现空指针异常

java - hibernate :org.hibernate.MappingException:未知实体:entity.Item

java - Realm 异步任务