Blanca Hdez
Blanca Hdez

Reputation: 3563

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

Upvotes: 2

Views: 23161

Answers (3)

Sridhar
Sridhar

Reputation: 1

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..

Upvotes: 0

tom
tom

Reputation: 2745

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>

Upvotes: 3

TheKojuEffect
TheKojuEffect

Reputation: 21101

${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}'/>

Upvotes: 1

Related Questions