Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring 3.0加载属性文件并设置@Scheduled cron_Java_Cron_Spring 3_Spring Scheduled - Fatal编程技术网

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

Java Spring 3.0加载属性文件并设置@Scheduled cron,java,cron,spring-3,spring-scheduled,Java,Cron,Spring 3,Spring Scheduled,我想每小时执行一种方法来完成我的工作 我正在使用Spring3.0(请记住)下面的ScheduleCron是我的代码。但它给出了以下错误。 错误:“bean初始化失败;嵌套异常为java.lang.IllegalArgumentException:无法解析占位符“scheduling.job.cron” ApplicationContext.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="

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

我正在使用Spring3.0(请记住)下面的ScheduleCron是我的代码。但它给出了以下错误。 错误:“bean初始化失败;嵌套异常为java.lang.IllegalArgumentException:无法解析占位符“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"/>
属性文件(存在于src/main/resource/test.Properties中)包含以下行

scheduling.job.cron=0 0/1 * * * ?
有人能帮我摆脱这个错误并成功地工作吗。
提前感谢。

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

即使您能够从test.properties文件解析property 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>

确保已正确配置应用程序。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>


查看此链接以更好地理解

谢谢您的回复,但我已在test.properties中给出了以下属性scheduling.job.cron=0 0/1***?更新了答案,请检查并让我知道。未解决。。。getting org.springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套异常为java.io.FileNotFoundException:无法打开类路径资源[resource/test.properties],因为它不存在。我尝试放置类路径*:/resource/*。属性仍然无法初始化bean;嵌套异常为java.lang.IllegalArgumentException:无法解析占位符'scheduling.job.cron'在我的计算机上尝试了您的代码,并用我的发现更新了答案,升级到spring 3.01版本以使用所需功能。由于我的test.properties位于maven项目下的resources文件夹中,因此我是否需要在位置上提供resources/test.properties?从技术上讲,spring应默认检查位于resources文件夹下的所有文件。我已尝试过此操作,但它不会像这样读取文件resources/test.properties或just.properties:(还有什么其他原因吗?您是否使用此代码来运行一些JUnit测试?没有,我没有任何JUnit测试用例..我有我的应用程序,将test.properties文件放在其中,并希望在此基础上进行调度。
<?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>