java Bean创建异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13092607/
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-10-31 11:27:20  来源:igfitidea点击:

Bean creation exception

javaspringhibernate

提问by JNPW

My application context looks likt this,

我的应用程序上下文看起来像这样,

<bean id="caseTxBo" class="gov.case.rcp.bo.impl.caseTxBoImpl" >
            <property name="caseTxDao" ref="caseTxDao" />
    </bean>
    <bean id="caseTxDao" class="gov.case.rcp.dao.impl.caseTxDaoImpl" >
            <property name="sessionFactory" ref="sessionFactory" />

Action class gets the context like this:

Action 类获取上下文如下:

ServletContext context = request.getSession().getServletContext();
     BeanFactory factory = WebApplicationContextUtils.
           getRequiredWebApplicationContext(context);
     CaseTxBOImpl caseTxBo = (

             caseTxBoImpl) factory.getBean("caseTxBo");

     List<caseTxPmt> errorVarList =       
     caseTxBo.getcaseTxDao().findAllcaseTx();

    model.put("caseTxList", caseTxList);

BOImpl implements Dao and DAOImpl implements Dao:

BOImpl 实现了 Dao,DAOImpl 实现了 Dao:

DaoImpl has the implementation and returns caseTxList.

DaoImpl 有实现并返回 caseTxList。

but I get a runTimeException as

但我得到一个 runTimeException 作为

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'caseTxBo' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Cannot resolve reference to bean 'caseTxDao' while setting bean property 'caseTxDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'caseTxDao' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [gov.case.rcp.pp.dao.impl.CaseTxDaoImpl]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
follows:
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)

Where did i go wrong?

我哪里做错了?

回答by Tomasz Nurkiewicz

Bean property 'sessionFactory'is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? follows: at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)

Bean 属性'sessionFactory'不可写或具有无效的 setter 方法。setter 的参数类型是否与 getter 的返回类型匹配?如下: 在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference( BeanDefinitionValueResolver.java:275)

The error message is pretty clear. Open your CaseTxDaoImpland make sure there is a following method there:

错误信息非常清楚。打开您的CaseTxDaoImpl并确保那里有以下方法:

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

BTW this:

顺便说一句:

ServletContext context = request.getSession().getServletContext();
BeanFactory factory = WebApplicationContextUtils.
       getRequiredWebApplicationContext(context);
factory.getBean("caseTxBo")

is pretty low-level usage of Spring and an anti-pattern. Are you using any web framework? Typically they integrate with Spring quite well.

是 Spring 的低级用法和反模式。你在使用任何网络框架吗?通常,它们与 Spring 集成得很好。