spring - bean JndiObjectFactoryBean 的 Spring NotWritablePropertyException 和无效属性“lazyInit”
问题描述
我们正在使用基于 WAS8.5 的 JNDI 数据源配置。服务器启动时,不会创建此数据源。因此抛出
org.springframework.beans.factory.BeanCreationException 通过说
“设置属性值时出错;嵌套异常是 org.springframework.beans.NotWritablePropertyException:lazyInit
bean 类 [org.springframework.jndi.JndiObjectFactoryBean] 的无效属性 '':Bean 属性'lazyInit
'是不可写或 setter 方法无效。setter 的参数类型是否与 getter 的返回类型匹配?
我们没有尝试lazyInit
在我们的应用程序中设置属性。这里可能是什么问题?这里有什么遗漏吗?
弹簧上下文.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 会话工厂 bean。但实际结果是得到以下异常。
org.springframework.beans.factory.BeanCreationException: 在类路径资源 [conf/xml/dao-spring-context.xml] 中定义
名称为“”的 bean 创建错误:在设置 bean 属性“”时sqlSessionFactory
无法解析对 bean“”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“ ”的 bean 时出错:设置属性值时出错;嵌套异常是 org.springframework.beans.NotWritablePropertyException:bean 类 [org.springframework.jndi.JndiObjectFactoryBean] 的无效属性“”:bean 属性“ ”不可写或具有无效的 setter 方法。setter 的参数类型是否与 getter 的返回类型匹配?ds_app1
dataSource
ds_app1
lazyInit
lazyInit
解决方案
这种方法对我有用
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 上下文 xml 中包含这个 bean
<bean id="customJndiInstantiationAwareBeanPostProcessor" class="org.test.CustomJndiInstantiationAwareBeanPostProcessor"/>
推荐阅读
- django - 查询集上的 Django 总和
- php - 使用 Phpspreadsheet 从数据库下载 excel 表时出错
- excel - 当特定单元格有数据时,如何锁定大部分行?
- c - C.printf中的问题("%ld%ld%ld\n",lol,&lol,*lol);
- swift - 先自动完成后启动另一个后台进程
- php - 如何使用嵌套为 Elasticsearch 创建关系数据库结构
- android - Android SDK 管理器不显示 Android SDK Platform_tools
- javascript - React Native 使用 auth() 时出错
- swift - 使用 watchOS 5 访问 Apple Watch 应用扩展上的用户默认值
- tensorflow - 修复要保留的模型检查点的数量 pytorch