java.lang.IllegalArgumentException: spring+hibernate 需要“sessionFactory”或“hibernateTemplate”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15045468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:45:05  来源:igfitidea点击:

java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required in spring+hibernate

springhibernate

提问by cherry

I am doing spring + hibernate apllication. When I run the application on tomcat server I am getting some exception. Below is my code.

我正在做 spring + hibernate 应用程序。当我在 tomcat 服务器上运行应用程序时,出现了一些异常。下面是我的代码。

This is my bean config file.

这是我的 bean 配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>WEB-INF/database/db.properties</value>
    </property>
</bean>
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource">
            <ref bean="dataSource" />
        </property> 
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>Employee.hbm.xml</value>
            </list>
        </property>
    </bean>
    <bean id="employeeBo" class="com.saggezza.employee.bo.impl.EmployeeBoImpl">
        <property name="employeeDao" ref="employeeDao" />
    </bean>

    <bean id="employeeDao" class="com.saggezza.employee.dao.impl.EmployeeDaoImpl">
        <constructor-arg ref="sessionFactory"></constructor-arg>
    </bean>

this is my dao class.

这是我的道课。

public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {

    private SessionFactory sessionFactory;
    public EmployeeDaoImpl(SessionFactory sessionfactory){
        this.sessionFactory=sessionfactory;
    }

    @Override
    public List<Employee> getEmployeeDetails() {
        return getHibernateTemplate().find("from Employee");

    }
}

Here another class employeeBo is calling the employeeDaoImpl. when I run thisI am getting the below exception.

这里另一个类employeeBo 正在调用employeeDaoImpl。当我运行这个时,我收到以下异常。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeBo' defined in ServletContext resource [/WEB-INF/spring/EmployeeBean.xml]: Cannot resolve reference to bean 'employeeDao' while setting bean property 'employeeDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao' defined in ServletContext resource [/WEB-INF/spring/EmployeeBean.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required

Can anybody help to resolve this. I have tried a lot and google it as well.But did get the solution.

任何人都可以帮助解决这个问题。我已经尝试了很多,也谷歌了它。但确实得到了解决方案。

采纳答案by Adisesha

If you have two configuration files, you duplicates 'sessionFactory' definition. Remove one of the 'sessionFactory' definitions . You would have got duplicate bean definition exception before the IllegalArgumentException.

如果您有两个配置文件,则复制“sessionFactory”定义。删除 'sessionFactory' 定义之一。在 IllegalArgumentException 之前,您会遇到重复的 bean 定义异常。

Edit: After your comment,

编辑:在您发表评论后,

public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {


public EmployeeDaoImpl(SessionFactory sessionfactory){
    setSessionFactory(sessionfactory);
}

@Override
public List<Employee> getEmployeeDetails() {
    return getHibernateTemplate().find("from Employee");

}
}

or get rid of constructor in above code and inject 'sessionFactory' using setter injection.See org.springframework.orm.hibernate3.support.HibernateDaoSupport.setSessionFactory(SessionFactory). I prefer later approach.

或摆脱上面代码中的构造函数并使用 setter injection.See 注入“sessionFactory” org.springframework.orm.hibernate3.support.HibernateDaoSupport.setSessionFactory(SessionFactory)。我更喜欢后来的方法。

回答by Sazzadur Rahaman

I think the problem is the type of SessionFactoryyou are injecting in EmployeeDaoImpldoes not match with the type of the SessionFactoryyou used in the class.

我认为问题在于SessionFactory您注入EmployeeDaoImpl的类型与SessionFactory您在课堂上使用的类型不匹配。

Can you check it?

你能检查一下吗?

回答by Keyur

This is an old question so must be solved now but still if someone comes across this problem. Following is solution.

这是一个老问题,所以现在必须解决,但如果有人遇到这个问题。以下是解决方案。

You can use Hibernate DAO Support by extending HibernateDAOSupport class and overriding its afterPropertiesSet() method.

您可以通过扩展 HibernateDAOSupport 类并覆盖其 afterPropertiesSet() 方法来使用 Hibernate DAO 支持。

This method is called in HibernateDAO support and at that time since sessionFactory is null it is throwing this error. In your custom class you can set this property explicitly and then call the same method of Parent Class (i.e. HibernateDAOSupport's addProperties() method)

此方法在 HibernateDAO 支持中被调用,当时由于 sessionFactory 为空,因此抛出此错误。在您的自定义类中,您可以显式设置此属性,然后调用父类的相同方法(即 HibernateDAOSupport 的 addProperties() 方法)

package com.techcielo.spring4.hibernate.template;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

@Component("hibernateTemplate")
public class Hibernate4CustomTemplate extends HibernateTemplate{

    @Autowired(required=true)
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory) {
        System.out.println("Setting SessionFactory");
        this.sessionFactory = sessionFactory;
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public void afterPropertiesSet() {
    System.out.println("Checking if properties set..."+this.sessionFactory);
    setSessionFactory(sessionFactory);
    super.afterPropertiesSet();
    }
}

Following can be used for sample!

以下可用于示例