Casting a String property to int in Spring config file - spring

I have a properties file test.properties
HOST = http://localhost
PORT = 1234
In my config file I am passing the values from the properties file to a bean constructor-arg.
<util:properties id="nodeProperty"
location="classpath:config/test.properties" />
<context:property-placeholder
properties-ref="nodeProperty" />
<bean id="client" class="com.abc.client.ReadWrite">
<constructor-arg value="${HOST}" index="0"/>
<constructor-arg value="${PORT}" index="1" type="int"/>
</bean>
When I run this I get the following error
Unsatisfied dependency expressed through constructor argument with index 1 of type
[int]: Could not convert constructor argument value of type [java.lang.String] to
required type [int]: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "8002;"

Look at the end of error message - For input string: "8002;"
I think it is incorrect value for PORT property.

Related

Convert property value of type 'java.lang.String' to required type 'org.springframework.expression.Expression' for property 'onFailureExpression'

I am facing below error and using spring version 5.3.14 and Spring_integration 5.5.7 and using camel version 2.25.4.
org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.expression.Expression' for property 'onFailureExpression'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.expression.Expression' for property 'onFailureExpression': no matching editors or conversion strategy found
config file:
<int:filter id="xpathfilter" input-channel="eventSpringXpathChannel"
output-channel="eventSpringOutChannel" discard-channel="eventSpringFailureChannel"
expression="#xpath(payload, headers.get('xpathKey'), 'boolean')">
<int:request-handler-advice-chain>
<bean class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
<property name="onFailureExpression" value="payload" />
<property name="failureChannel" ref="eventSpringXpathErrorChannel" />
<property name="trapException" value="true" />
</bean>
</int:request-handler-advice-chain>
</int:filter>
That setter expects an instance of org.springframework.expression.Expression, but you provide just value="payload", which is indeed not an Expression.
See another setter:
/**
* Set the expression to evaluate against the root message after a failed
* handler invocation. The exception is available as the variable {#code #exception}.
* Defaults to {#code payload}, if {#code failureChannel} is configured.
* #param onFailureExpression the SpEL expression.
* #since 4.3.7
*/
public void setOnFailureExpressionString(String onFailureExpression) {
So, you config will look like:
<property name="onFailureExpressionString" value="payload" />

Getting : java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource]

I am using Spring 3.2.13 jars and ibatis 2.3.4.726 version to set up a dataSource as below
I am getting the below Exception
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
My xml Spring Configurations are as below :
<bean id="commonDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/ds_GOLD" />
<property name="defaultObject" value="jdbc/ds_SILVERR" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>
classpath:com/dav/dao/config/sqlMapConfig.xml
</value>
</property>
<property name="dataSource">
<ref bean="commonDataSource" />
</property>
<property name="transactionConfigClass">
<value>com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig
</value>
</property>
<property name="transactionConfigProperties">
<props>
<prop key="DefaultAutoCommit">false</prop>
<prop key="SetAutoCommitAllowed">false</prop>
</props>
</property>
</bean>
The Configurations seems good for me but getting exception
only if the "jdbc/ds_GOLD" is not configured in App server. In this case it should get picked the defaultObject JNDI. If the "jdbc/ds_GOLD" is configured in App server i don't see any error in logs. But Failover case it is not working.
[5/1/16 3:32:51:329 EDT] 00000098 webapp I com.ibm.ws.webcontainer.webapp.WebApp log SRVE0296E: [glow#glow.war][/glow][Servlet.LOG]:.StrutsServlet: null:.org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gcdSqlMapClient' defined in ServletContext resource [/WEB-INF/config/common-dao.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:618)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934)
.
.
.
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1862)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:464)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:495)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:489)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1465)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1424)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
... 114 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:449)
... 120 more
``
Forget to keep the defaultObject as property reference to the dataSource. I wrongly configured it as property value.

char[] to String Conversion in Spring configuration

I have bean A that returns an attribute as char[] and another bean that expects an attribute as String and I would like to inject the attribute from bean A to bean B.
When I do this:
<bean id="B" class="....">
<property name="password" value="#{A.password}" />
</bean>
<bean id="A" class="...">
</bean>
The error I'm getting is:
Cannot convert value of type [char[]] to required type [java.lang.String] for property 'password': no matching editors or conversion strategy found
Any idea how to resolve this?
Perhaps by using an expression language syntax?
Perhaps by registering some sort of a converter like org.springframework.beans.propertyeditors.CharArrayPropertyEditor in the configuration before injecting the char[] attribute?
Looks like this will work:
<property name="password" value="#{new java.lang.String(A.password)}" />

spring 3.0 MVC seems to be ignoring messages.properties

Spring 3.0 MVC
First of all, I haven't found any documentation regarding messages.properties # springsource Everything I've found about overriding error messages has been on various forums. If anyone has a reference to where messages.properties is documented that'd be fantastic. Maybe messages.properties comes not from spring but a java spec?
I have tried following the advice on
JSR-303 Type Checking Before Binding My goal is to replace some type mismatch error messages with my own user friendly error messages
My situation is as follows:
Model
public class Test {
private int numberbomb;
public int getNumberbomb() {
return numberbomb;
}
public void setNumberbomb(int numberbomb) {
this.numberbomb = numberbomb;
}
}
myservlet.xml
<mvc:annotation-driven/>
jsp
<form:form id="test" method="post" modelAttribute="test">
<form:errors path="*"/>
<form:label path="numberbomb">numberbomb</form:label>
<form:input path="numberbomb"/>
</form:form>
classes\messages.properties
typeMismatch=bad value you bad bad person
test.numberbomb=you are driving me crazy with these bad inputs
Output from the form
Failed to convert property value of type java.lang.String to required type int for property numberbomb; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "three" from type java.lang.String to type int; nested exception is java.lang.NumberFormatException: For input string: "three"
BindingResult.toString() within my controller
Field error in object 'test' on field 'numberbomb': rejected value [three]; codes [typeMismatch.test.numberbomb,typeMismatch.numberbomb,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [test.numberbomb,numberbomb]; arguments []; default message [numberbomb]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'numberbomb'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "three" from type 'java.lang.String' to type 'int'; nested exception is java.lang.NumberFormatException: For input string: "three"]
Is displaying the error messages with <form:errors> the wrong way display custom error messages? Do I need to add something to spring config files to tell it to look at messages.properties? Spring seems to be ignoring my messages.properties file (which is located in the WEB-INF\classes folder)
Thanks for any ideas!
An associate of mine pointed me in the right direction. I changed the messageSource bean in myservlet.xml
from
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="cacheSeconds" value="1" />
</bean>
to
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>
For whatever reason this solved the problem. Thanks associate! :)

BeanInstantiationException:Cannot convert type from java.lang.String to required type --- idref tag

I am new to spring and trying to understand the functionality of the idref tag
My config file is pasted here:
<bean id="AudioSystembean" class="com.springexample.AudioSystem">
<property name="price" value="200"/>
</bean>
<bean id="Vehicle1" class="com.SpringExample.Vehicle1">
<property name="vehicleType" value="CAR" />
<property name="audioSystem" >
<idref bean = "AudioSystembean" />
</property>
</bean>
When I am executing the code I am getting the error as : "BeanInstantiationException:Cannot convert type from java.lang.String to required type :com.SpringExampl.AudioSystem" [I dont remember the exact exception - I have the code at home]
If I use ref instead of idref it is working fine.
I tried google to understand about idref but could not get much information..
What am I doing wrong here?
In a Spring context, <idref> is used to pass the name of the referenced bean, rather than the bean itself (which is what <ref> does). So in your example:
<property name="audioSystem" >
<idref bean = "AudioSystembean" />
</property>
The audioSystem system property will be injected with the name of the AudioSystembean, i.e. the String "AudioSystembean".
This is not what you need here, though, you need the <ref> element, which passes a reference to the bean itself.

Resources