调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:'sessionFactory'或'hibernateTemplate'是必需的

时间:2017-05-30 09:15:45

标签: java spring

当我运行以下代码时,我收到错误“调用init方法失败;嵌套异常是java.lang.IllegalArgumentException:'sessionFactory'或'hibernateTemplate'是必需的”我的代码中的错误是什么?

EmployeeDaoImpl.java

@Repository
public class EmployeeDaoImpl extends AbstractHibernateDao implements EmployeeDao{
public Employee getSalaryDetails(Integer empId)
{
    Session session = getSession();
    Query query = session.getNamedQuery("getEmpSalaryDtls");
    query.setInteger("empId", empId);
    query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    Map map = (Map) query.uniqueResult();
    closeSession(session);
    return map;
}

AbstractHibernateDao.java

public abstract class AbstractHibernateDao extends HibernateDaoSupport {
public Session getSession() {       
    HibernateTemplate template = this.getHibernateTemplate();       
    template.setSessionFactory(getSessionFactory());
    return template.getSessionFactory().openSession();
}

错误信息是:

"192.24.0.77" "sourcePrg" 2017-05-30 04:41:14 "org.springframework.web.struts.ContextLoaderPlugIn" "Context initialization failed"
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'EmployeeDaoImpl' defined in file [D:\....\EmployeeDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.web.struts.ContextLoaderPlugIn.createWebApplicationContext(ContextLoaderPlugIn.java:355) ~[spring-struts-3.2.18.RELEASE.jar:3.2.18.RELEASE]
at org.springframework.web.struts.ContextLoaderPlugIn.initWebApplicationContext(ContextLoaderPlugIn.java:296) ~[spring-struts-3.2.18.RELEASE.jar:3.2.18.RELEASE]
at org.springframework.web.struts.ContextLoaderPlugIn.init(ContextLoaderPlugIn.java:226) ~[spring-struts-3.2.18.RELEASE.jar:3.2.18.RELEASE]
at org.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:1158) ~[struts-1.1.jar:1.1]
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:473) ~[struts-1.1.jar:1.1]
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:456) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.internal.AsyncRunLevelContext.findOrCreate(AsyncRunLevelContext.java:225) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.RunLevelContext.findOrCreate(RunLevelContext.java:82) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2488) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:98) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.oneJob(CurrentTaskFuture.java:1162) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.run(CurrentTaskFuture.java:1147) ~[weblogic.server.merged.jar:12.1.3.0.0]
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$UpOneLevel.run(CurrentTaskFuture.java:753) ~[weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:548) ~[weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311) ~[weblogic.server.merged.jar:12.1.3.0.0]
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263) ~[weblogic.server.merged.jar:12.1.3.0.0]

 Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate5.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:117) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) ~[spring-tx-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]

现在我收到了这个错误:

"org.springframework.web.struts.ContextLoaderPlugIn" "Context initialization failed"
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'EmployeeServiceImpl': Unsatisfied dependency expressed through field 'EmployeeDao'; nested exception is 
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'EmployeeDao' defined in ServletContext resource [/WEB-INF/spring-beans.xml]: Error setting property values; nested exception is 
 org.springframework.beans.NotWritablePropertyException: Invalid property 'hibernateTemplate' of bean class [com.temp.EmployeeDaoImpl]: Bean property 'hibernateTemplate' is not writable or has an invalid setter method. 
 Does the parameter type of the setter match the return type of the getter?

1 个答案:

答案 0 :(得分:2)

自hibernate 3.0.1以来,不鼓励使用HibernateTemplate,它引入了对上下文会话的支持(大约在2008年!)。如今,我们鼓励/建议您针对the plain Hibernate API撰写DAO。

然后说要将你的DAO重写为以下内容。

public abstract class AbstractHibernateDao {

    @Autowired
    private SessionFactory sessionFactory;

    protected Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }
}

现在你实际实施的DAO

@Repository
public class EmployeeDaoImpl extends AbstractHibernateDao implements EmployeeDao{
public Employee getSalaryDetails(Integer empId)
{
    Session session = getCurrentSession();
    Query query = session.getNamedQuery("getEmpSalaryDtls");
    query.setInteger("empId", empId);
    query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    Map map = (Map) query.uniqueResult();
    return map;
}

这里有几个假设。

  1. 您使用HibernateTransactionManager
  2. 设置了正确的交易
  3. 您拥有@Transactional
  4. 服务的实际交易
  5. 您已启用@EnableTransactionManagement<tx:annotation-driven />的交易。
  6. 这个Spring负责为你打开/关闭Session,你不必再考虑它了。