Spring 3.0 加载属性文件并设置@Scheduled cron

Spring 3.0 load proprrties file and set @Scheduled cron

我想每小时执行一个方法来完成我的工作。

我正在使用 SPRING 3.0(请记住)下面的计划 cron 是我的代码。但它给出了以下错误。 错误:"Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'scheduling.job.cron'"

ApplicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/task 
   http://www.springframework.org/schema/task/spring-task-3.0.xsd
   http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util.xsd ">

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
    <list>
        <value>classpath:test.properties</value>            
    </list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>

Java Class

@Scheduled(cron = "${scheduling.job.cron}")
public void testScheule()
{
    logger.info("Schedule Call" + new Date());
}

属性文件(存在于 src/main/resource/test.properties 中)包含以下行

scheduling.job.cron=0 0/1 * * * ?

有人可以帮助我摆脱这个错误并成功地工作吗? 提前致谢。

您想要实现的目标在 Spring 3.0 中是不可能的。
此功能可用,从 spring 3.0.1.

开始

即使您能够从 test.properties 文件中解析 属性 scheduling.job.cron,您也会收到以下异常:-

java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in ${scheduling.job.cron})

在 Spring 3.0 中,唯一可行的方法是:-

@Service (value="testService")
public class TestService {
// ..

  public void testScheule()  { .. }

}

现在您可以在 xml 中定义如下:-

 <context:component-scan base-package="com.some.package" />
    <task:annotation-driven />
    <util:properties id="applicationProps" location="test.properties" />

<task:scheduled-tasks>
  <task:scheduled ref="testService " method="testScheule" cron="#{applicationProps['scheduling.job.cron']}" />
</task:scheduled-tasks>

确保您已正确配置 application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:task="http://www.springframework.org/schema/task"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <task:annotation-driven />
    <util:properties id="testProps" location="test.properties" />
    <context:property-placeholder properties-ref="testProps" />
    <bean id="yourBeanClassHere" class="com.howtodoinjava.service.YourBeanClassImplHere"></bean>
</beans>

看看这个 link 以便更好地理解 http://howtodoinjava.com/spring/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/