本文介绍了java.lang.IllegalArgumentException:'sessionFactory'或'hibernateTemplate'在spring + hibernate中是必需的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的配置文件。

 <?xml version =1.0encoding =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>
<值> WEB-INF / database / db.properties< /值>
< / property>
< / bean>
< bean id =dataSource
class =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =$ {jdbc.driverClassName}/>
< property name =urlvalue =$ {jdbc.url}/>
< property name =usernamevalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< / bean>

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

< property name =dataSource>
< ref bean =dataSource/>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property>
< property name =mappingResources>
< list>
<值> Employee.hbm.xml< /值>
< / list>
< / property>
< / bean>
< bean id =employeeBoclass =com.saggezza.employee.bo.impl.EmployeeBoImpl>
< property name =employeeDaoref =employeeDao/>
< / bean>

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

这是我的dao类。

 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);


$ b

这里的另一个班员employeeBo正在调用employeeDaoImpl。
当我运行这个我得到以下例外。

  org.springframework.beans.factory.BeanCreationException:创建错误在ServletContext资源中定义名为'employeeBo'的bean [/WEB-INF/spring/EmployeeBean.xml]:设置bean属性'employeeDao'时,无法解析对bean'employeeDao'的引用;嵌套的异常是org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/spring/EmployeeBean.xml]中定义名称为'employeeDao'的Bean时创建错误:调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:'sessionFactory'或'hibernateTemplate'是必需的

任何人都可以帮忙解决这个问题。如果您有两个配置文件,那么您可以使用这个配置文件来解决这个问题。

重复'sessionFactory'的定义。删除'sessionFactory'定义之一。你会在IllegalArgumentException之前得到重复的bean定义异常。



编辑:在你的评论之后,

  public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {

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

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


$ b $ / code $ / pre

或者去掉上面的构造函数代码并使用setter注入来注入'sessionFactory'。参见 org.springframework.orm.hibernate3.support.HibernateDaoSupport.setSessionFactory(SessionFactory)。我更喜欢以后的做法。


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

This is my bean config file.

<?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.

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.

解决方案

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.

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.

这篇关于java.lang.IllegalArgumentException:'sessionFactory'或'hibernateTemplate'在spring + hibernate中是必需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:05