我正在做spring+hibernate繁殖。当我在tomcat服务器上运行该应用程序时,出现了一些异常。下面是我的代码。
这是我的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>
这是我的道课。
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"); } }
在这里,另一个班级的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
任何人都可以帮助解决此问题。我已经尝试了很多并且也用谷歌搜索了它,但是确实找到了解决方案。
如果有两个配置文件,则复制“ sessionFactory”定义。删除“ sessionFactory”定义之一。在IllegalArgumentException之前,您将有重复的bean定义异常。
编辑:发表评论后,
public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao { public EmployeeDaoImpl(SessionFactory sessionfactory){ setSessionFactory(sessionfactory); } @Override public List<Employee> getEmployeeDetails() { return getHibernateTemplate().find("from Employee"); } }
或在上面的代码中删除构造函数,并使用setter注入来注入“ sessionFactory” org.springframework.orm.hibernate3.support.HibernateDaoSupport.setSessionFactory(SessionFactory)。请参见。我喜欢以后的方法。
org.springframework.orm.hibernate3.support.HibernateDaoSupport.setSessionFactory(SessionFactory)