Spring NotWritablePropertyException和bean JndiObjectFactoryBean

时间:2019-04-03 11:58:40

标签: spring datasource jndi

我们正在使用基于WAS8.5的JNDI数据源配置。服务器启动时,不会创建此数据源。因此抛出

  

org.springframework.beans.factory.BeanCreationException,方法是说
“错误设置属性值;嵌套的异常是org.springframework.beans.NotWritablePropertyException:Bean类[org.net]的无效属性“ lazyInit”。 springframework.jndi.JndiObjectFactoryBean]:Bean属性“ lazyInit”不可写或具有无效的setter方法。setter的参数类型是否与getter的返回类型匹配?”

我们不尝试在应用程序中设置lazyInit属性。这里可能是什么问题?这里有什么错过的吗?

Spring-context.xml

<beans
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.3.xsd
  http://www.springframework.org/schema/jee
  http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans">

  <context:annotation-config/>

  <jee:jndi-lookup id="ds_app1" jndi-name="java:comp/env/jdbc/ds_app1" />

  <!-- SQL Session factories -->
  <bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean">
    <property ref="ds_app1" name="dataSource"/> 
    <property name="configLocation" value="classpath:/conf/xml/mybatis-config.xml" />
    <property name="mapperLocations" value="classpath:/conf/xml/mapper/*.xml"/> 
  </bean> 

</beans>

同一段代码在具有相同WAS8.5服务器和相同数据源配置的另一个环境中工作。在我们的应用程序中,我们使用spring4.3.8,mybatis3.x和java8。在这里,我们使用xml配置(dao-spring-context.xml)注入数据源bean

预期的输出是应该将数据源注入sql session factory bean。但是实际结果得到以下异常。

  

org.springframework.beans.factory.BeanCreationException:   
  创建在类路径资源[conf / xml / dao-spring-context.xml]中定义的名称为'sqlSessionFactory'的bean时出错:设置bean属性'{{时,无法解析对bean'ds_app1'的引用1}}';嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'dataSource'的bean时出错:设置属性值时出错;嵌套的异常是org.springframework.beans.NotWritablePropertyException:Bean类[org.springframework.jndi.JndiObjectFactoryBean]的无效属性“ ds_app1”:Bean属性“ lazyInit”不可写或具有无效的设置器方法。   setter的参数类型是否与getter的返回类型匹配?

1 个答案:

答案 0 :(得分:1)

这种方法对我有用

1)创建一个后处理器

package org.test;

import org.springframework.bean.PropertyValue;
import org.springframework.bean.PropertyValues;
import org.springframework.bean.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import java.beans.PropertyDescriptor;

@Component
public class CustomJndiInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
     @Override
     public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) {
          if (bean instanceOf org.springframeworkf.jndi.JndiObjectFactoryBean) {
             for (PropertyValue pv: pvs.getPropertyValues()) {
                 if ("lazyInit".equals(pv.getName())) {
                    pv.setOptional(true);
                 }
             }
          }
     }
}

2)将此bean包含在您的Spring上下文xml中

<bean id="customJndiInstantiationAwareBeanPostProcessor" class="org.test.CustomJndiInstantiationAwareBeanPostProcessor"/>