JndiObjectFactoryBean的Spring NotWritablePropertyException和无效属性 "lazyInit"。

问题描述 投票:1回答:1

我们使用的是基于WAS8.5的JNDI数据源配置。当服务器启动时,这个数据源没有被创建。因此抛出

org.springframework.beans.factory.BeanCreationException通过说 "错误设置属性值;嵌套异常是org.springframework.beans.NotWritablePropertyException。无效属性'lazyInit'的bean类[org.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: 创建名称为''的Bean时出错。sqlSessionFactory'定义在类路径资源 [confxmldao-spring-context.xml] 中。无法解析对bean''的引用。ds_app1',同时设置bean属性'。dataSource'; 嵌套异常是org.springframework.beans.factory.BeanCreationException。创建名称为''的Bean时出错。ds_app1': 错误设置属性值;嵌套异常是org.springframework.beans.NotWritablePropertyException。无效属性'lazyInit'的bean类[org.springframework.jndi.JndiObjectFactoryBean]。Bean属性'lazyInit'是不可写的或有一个无效的setter方法。 setter的参数类型与getter的返回类型是否匹配?

spring datasource jndi
1个回答
0
投票

这个方法对我来说是有效的

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) 在你的spring context xml中包含这个Bean。

<bean id="customJndiInstantiationAwareBeanPostProcessor" class="org.test.CustomJndiInstantiationAwareBeanPostProcessor"/>
© www.soinside.com 2019 - 2024. All rights reserved.