1
votes

We are using WAS8.5 based JNDI data source configuration. While server start up, this datasource is not getting created. Hence throwing

org.springframework.beans.factory.BeanCreationException by saying
"Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'lazyInit' of bean class [org.springframework.jndi.JndiObjectFactoryBean]: Bean property 'lazyInit' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?"

We are not trying to set lazyInit property in our application. What could be the issue here? Is anything missed here?

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>

Same piece of code is working in another environment with same WAS8.5 server with same set of data source configurations. In our application, we are using spring4.3.8,mybatis3.x and java8. Here we are injecting datasource beans using xml configuration(dao-spring-context.xml)

Expected output would be data source should be injected to sql session factory bean.but actual result is getting the below exception.

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sqlSessionFactory' defined in class path resource [conf/xml/dao-spring-context.xml]: Cannot resolve reference to bean 'ds_app1' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ds_app1': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'lazyInit' of bean class [org.springframework.jndi.JndiObjectFactoryBean]: Bean property 'lazyInit' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

1
How did you solve it?Tao Lu

1 Answers

1
votes

This approach worked for me

1) Create a post processor

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) Include this bean in your spring context xml

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