Invalid property 'connectionProperties' of bean class [org.apache.commons.dbcp.BasicDataSource] - java

I am getting this exception when initializing BasicDataSource. I want to pass Custom connection property to the dataSource.
My config is:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'connectionProperties' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'connectionProperties' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1042)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:902)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1420)

I used this post from spring forum to resolve the issue.
Adding this bean solved my issue.
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
lazy-init="false">
<property name="targetObject" ref="csdbDataSource" />
<property name="targetMethod" value="addConnectionProperty" />
<property name="arguments">
<list>
<value>defaultRowPrefetch</value>
<value>1</value>
</list>
</property>
</bean>

Related

Spring how to set proxy property dynamically to rest template

I am trying to set the proxy to spring rest template bean based on the property, I tried below configuration,
<bean id="forwardProxy" class="java.net.Proxy">
<constructor-arg>
<util:constant static-field="java.net.Proxy.Type.HTTP"/>
</constructor-arg>
<constructor-arg>
<bean class="java.net.InetSocketAddress">
<constructor-arg value="${forward.proxy.host}"/>
<constructor-arg value="${forward.proxy.port}"/>
</bean>
</constructor-arg>
</bean>
<util:constant id="noProxy" static-field="java.net.Proxy.NO_PROXY" />
<bean id="myRestTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean class="org.springframework.http.client.SimpleClientHttpRequestFactory">
<property name="connectTimeout" value="5000"/>
<property name="readTimeout" value="5000"/>
<property name="proxy" ref="#{ ${aem.enableProxy}==true ? forwardProxy : noProxy }"/>
</bean>
</constructor-arg>
</bean>
but I am getting below error saying No bean named 'DIRECT' available
Caused by (repeated) ... : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRestTemplate' defined in class path resource [environment/application.xml]:
Cannot create inner bean 'org.springframework.http.client.SimpleClientHttpRequestFactory#1177bca' of type [org.springframework.http.client.SimpleClientHttpRequestFactory] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.http.client.SimpleClientHttpRequestFactory#1177bca' defined in class path resource [environment/application.xml]:
Cannot resolve reference to bean '#{ false==true ? forwardProxy : noProxy }' while setting bean property 'proxy'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'DIRECT' available
Proxy.Type.DIRECT is a enum, not sure how to create a bean for that. Is it possible to achieve what I am doing without creating a factory beans option.
Using spring version: 4.3.8

Reasons of Invalid property 'dao' of bean class

In applicationContext_dao I got bean classNameDao, and now I am trying to create a BUS layer and add to applicationContext_bus two beans.
<bean id="classNameValidator" class="cz.mikros.w4.model.bus.validator.ClassNameValidator">
<property name="dao" ref="classNameDao" />
</bean>
<bean id="classNameBus" class="cz.mikros.w4.model.bus.impl.ClassNameBusImpl">
<property name="dao" ref="classNameDao" />
<property name="validator" ref="classNameValidator" />
</bean>
problem is
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'dao' of bean class [path.model.bus.validator.ClassNameValidator]: Bean property 'dao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Can you navigate me where can be the problem?
Check if your path.model.bus.validator.ClassNameValidator has public setter method on dao. It should be called public void setDao(DAO dao)

I can not have two datasources configured JPA Spring XML WildFly

Spring config in xml
<jee:jndi-lookup id="datas1" expected-type="javax.sql.DataSource"
jndi-name="java:/jndi1" />
<jee:jndi-lookup id="datas2" expected-type="javax.sql.DataSource"
jndi-name="java:/jndi2" />
<bean id="EntityM"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datas1" />
<property name="packagesToScan" value="package.persistence.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle12cDialect" />
</bean>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="EntityM" />
</bean>
The Excpetion is:
Context initialization failed: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSessionJdbcOperations' defined in class path resource [org/springframework/session/jdbc/config/annotation/web/http/JdbcHttpSessionConfiguration.class]: Unsatisfied dependency expressed through method 'springSessionJdbcOperations' parameter 0: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: datas1,datas2; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: datas1,jndi2.
I can not have two datasources configured, The first datasource is to the entityManagar and second datasource is to call stored procedure.
The JdbcSessionConfiguration expects a bean of type 'DataSource' with name 'dataSource'.
If this cannot be found, spring tries to use a bean of type 'DataSource', ignoring the name. Because there are two beans of this type, the exception is thrown.
Solution :
Rename the DataSource bean, that you want to use with the EntityManager from
'datas1' to 'dataSource'.
Spring should than be able to create JdbcSessionConfiguration and you can use the 'datas2' for whatever you need.

BeanCreationException hibernate5.LocalSessionFactoryBean Bean property 'EL' is not writable or has an invalid setter method

I am migrating from Hibernate 3/Spring 3 to Hibernate 5/Spring 4 and am receiving this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'sessionFactory-ETL' defined in class path resource
[config/context-hm.xml]: Error setting property values; nested
exception is org.springframework.beans.NotWritablePropertyException: Invalid
property 'eL' of bean class
[org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Bean property
'eventListeners' is not writable or has an invalid setter method. Does the
parameter type of the setter match the return type of the getter?
Here is part of the config file:
<bean name="hibernateEventListeners" class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
<map>
<entry key="pre-update" value-ref="pup" />
<entry key="save-update" value-ref="xSaS" />
<entry key="post-insert" value-ref="xIS" />
<entry key="merge" value-ref="xML" />
<entry key="pre-delete" value-ref="eRUL" />
</map>
</property>
</bean>
<bean id="sessionFactory-ETL"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="eventListeners" ref="hibernateEventListeners" />
Can anyone shed some light on this? I'm not sure if I need to change the type of the hibernateEventListeners to something other than a MapFactoryBean.

Error creating bean with name defined in class path resource [application-context.xml] in spring framework

<bean id="MyDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg value="MyDataSource"/>
</bean>
Error creating bean with name 'template' defined in class path
resource [application-context.xml]: Could not resolve matching
constructor (hint: specify index/type/name arguments for simple
parameters to avoid type ambiguities)
I am not sure what I am doing wrong here to get above error? Have all of it defined in properties file correctly with correct variable name. what are the things to check for ?
change
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg value="MyDataSource"/>
</bean>
to
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="MyDataSource"/>
</bean>
because you don't want to inject String value you want to inject referred bean
I got the same error and in my bean class, I declared the default constructor which was not declared earlier and it solved my problem.
So My complete error is like
Error creating bean with name 'student1' defined in class path resource [config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.springcore.springcore.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.springcore.springcore.Student.()

Categories