2

我正在以这种方式在spring中配置调度程序任务:

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

该属性period是在某个配置文件中设置的,默认类型好像是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"

我怎么能在这一步从Stirng变成Long??

提前致谢

编辑 占位符配置没有问题,我在另一个 bean 中使用了这个配置文件中的更多值。宣言:

period=30000
4

3 回答 3

3

有两种方法可以做到这一点:

1:更改您的方法以接受 java.lang.Long

2:在spring中自己创建一个java.lang.Long:

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

或没有额外的豆子

<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>
于 2013-09-18T12:58:53.060 回答
1

${period}被读取为 aString而不是${period}ie的值period被赋值为 value ${period}

要使此类属性起作用,您需要属性占位符。将此添加到配置中

<context:property-placeholder location='period.properties'/> 
// Edit location

然后你可以拥有

<property name="period" value='${period}'/>
于 2013-09-18T11:55:00.583 回答
0

可能发生的情况是您拼错了加载属性文件的类的完全限定名称。因此,spring 正在尝试将占位符字符串(即“${period}”)转换为 int,因此出现错误....

我曾经在使用代码时遇到同样的错误

两个地方有错别字。。

于 2014-01-22T14:47:18.397 回答