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

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" />

Related

Casting a String property to int in Spring config file

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.

custom thymeleaf dialect registration causing exception

in applicationContext.xml
<bean id="templateEngine"
class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalDialects">
<set>
<bean class="org.test.custom.CustomDialact" />
</set>
</property>
</bean>
Stacktrace:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'additionalDialects' of bean class [org.thymeleaf.spring3.SpringTemplateEngine]: Bean property 'additionalDialects' 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:1424)
... 50 more
i don't know but it was fixed after restarting machine. but mean while i find another solution
by extend a custom class with HashSet and in constructor add custom dialect in it and declare it in xml. provide it reference to additionalDialects . thats it .it will start running . it think it is problem related to generics as spring sometimes supply empty type
like HashSet<V> .

Spring configuration: long value out of a String property

I am configuring a scheduler task in spring in this way:
<bean id="someSchedulerTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
<!-- start after 60 seconds -->
<property name="delay" value="6000"/>
<!-- depends on the enviroment -->
<property name="period" value="${period}"/>
<property name="runnable" ref="myScheduler"/>
</bean>
The property period is set up in some configuration file, and it seems that the default type is String:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someSchedulerTask' defined in class path resource [context.xml]: Initialization of b
ean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'long' for property 'period'; nested exception is ja
va.lang.NumberFormatException: For input string: "period"
How could I change in this step from Stirng to Long??
Thanks in advance
EDIT
There is no problem with the place holder configuration, I am using more values from this config file in another beans.
Declaration:
period=30000
There are two ways to do this:
1: Change your method to accept a java.lang.Long
2: Create a java.lang.Long yourself in spring:
<bean id="period" class="java.lang.Long">
<constructor-arg index="0" value="${period}"/>
</bean>
<bean id="someSchedulerTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
<!-- start after 60 seconds -->
<property name="delay" value="6000"/>
<!-- depends on the enviroment -->
<property name="period" ref="period"/>
<property name="runnable" ref="myScheduler"/>
</bean>
or without the extra bean
<bean id="someSchedulerTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">
<!-- start after 60 seconds -->
<property name="delay" value="6000"/>
<!-- depends on the enviroment -->
<property name="period">
<bean class="java.lang.Long">
<constructor-arg index="0" value="${period}"/>
</bean>
</property>
<property name="runnable" ref="myScheduler"/>
</bean>
${period} is being read as a String instead of value of ${period} i.e period is being assigned with value ${period}.
For such properties to work, you need Property Placeholder. Add this to configuration
<context:property-placeholder location='period.properties'/>
// Edit location
Then you can have
<property name="period" value='${period}'/>
What is probably happening is you have misspelled the fully qualified name of the class which loads the properties file.Hence spring is trying the convert the place holder string i.e "${period}" to int hence the error....
I used to get the same error while using the code
There were typos in two places..

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)}" />

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