Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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 Cron表达式必须由6个字段组成(在“${Cron.expression}”中找到1个字段),这些字段部分已修复,但在AnnotationConfigApplicationContext中存在问题_Java_Spring Mvc_Cron_Spring Annotations_Spring Scheduled - Fatal编程技术网

Java Cron表达式必须由6个字段组成(在“${Cron.expression}”中找到1个字段),这些字段部分已修复,但在AnnotationConfigApplicationContext中存在问题

Java Cron表达式必须由6个字段组成(在“${Cron.expression}”中找到1个字段),这些字段部分已修复,但在AnnotationConfigApplicationContext中存在问题,java,spring-mvc,cron,spring-annotations,spring-scheduled,Java,Spring Mvc,Cron,Spring Annotations,Spring Scheduled,我正在尝试参数化cron表达式并从属性文件中读取它。在此过程中,我在创建名为“springScheduleCronExample”的bean时遇到以下异常错误:初始化bean失败;嵌套异常为java.lang.IllegalStateException:遇到无效的@Scheduled方法“cronJob”:cron表达式必须包含在${cron.expression}中找到1个字段,共6个字段 然后我发现了下面的内容 使用该I-cron表达式进行读取,前提是 AnnotationConfigApp

我正在尝试参数化cron表达式并从属性文件中读取它。在此过程中,我在创建名为“springScheduleCronExample”的bean时遇到以下异常错误:初始化bean失败;嵌套异常为java.lang.IllegalStateException:遇到无效的@Scheduled方法“cronJob”:cron表达式必须包含在${cron.expression}中找到1个字段,共6个字段

然后我发现了下面的内容

使用该I-cron表达式进行读取,前提是

AnnotationConfigApplicationContext上下文=新的AnnotationConfigApplicationContext SpringScheduleCronExample.class

在我的main方法中定义。我遇到的问题是,我想在没有main方法的服务器上运行这个,有人能帮我吗

这是我的applicationContext.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="applicationProps" location="application.properties" />
    <context:property-placeholder properties-ref="applicationProps"  />
    <bean class="com.hemal.spring.SpringScheduleCronExample" />
</beans>
<?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">


    <context:property-placeholder properties-ref="applicationProps"  />
    <context:annotation-config/>
    <context:component-scan base-package="com.hemal.spring" />
    <task:annotation-driven />
</beans>
我的应用程序属性已更改
cron.expression=*/5****?

下面是我如何让它工作的

Application-context.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="applicationProps" location="application.properties" />
    <context:property-placeholder properties-ref="applicationProps"  />
    <bean class="com.hemal.spring.SpringScheduleCronExample" />
</beans>
<?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">


    <context:property-placeholder properties-ref="applicationProps"  />
    <context:annotation-config/>
    <context:component-scan base-package="com.hemal.spring" />
    <task:annotation-driven />
</beans>
MyApplicationContext.java

包com.hemal.spring

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class MyApplicationContext implements WebApplicationInitializer{

    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(MyApplicationConfig.class);


        servletContext.addListener(new ContextLoaderListener(rootContext));
    }
}
我的调度程序类

package com.hemal.spring;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;


public class SpringScheduleCronExample {
    private AtomicInteger counter = new AtomicInteger(0);

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {

        return new PropertySourcesPlaceholderConfigurer();
    } 

    @Scheduled(cron = "${cron.expression}")
    public void cronJob() {
        int jobId = counter.incrementAndGet();
        System.out.println("Job @ cron " + new Date() + ", jobId: " + jobId);
    }

    public static void main(String[] args) {
           AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
                   SpringScheduleCronExample.class);
        try {
            Thread.sleep(24000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
           context.close();
        }
    }
}
应用程序属性
cron.expression=0/5****?

您知道如何创建web应用程序吗?如果不知道,您可以查看中的前两个步骤,以获得一个简单的示例。如果您有一个web应用程序,您需要将spring依赖项添加到pom.xml中,并在web.xml中指定spring上下文,然后检查。Czerwinski,我知道如何创建web应用程序。我的应用程序没有GUI,它通过MDB处理消息并进行web服务调用。如果web服务调用中出现故障,而不是使用错误队列再次处理这些消息,我希望每4小时运行一次spring调度程序,以进行web服务调用。我已使此过程正常工作。我希望将cron表达式移动到属性文件并执行fa在这个过程中遇到了很多挑战。我仍然不太清楚您面临着什么问题。在服务器上运行应用程序时,您的applicationContext.xml和application.properties文件是否在类路径上?如果是并且正确实例化了上下文,请查看第一条注释中的最后一个链接,那么您的计划程序应该可以正常工作问题是上下文没有被正确实例化。我相应地修改了我的代码,它可以工作。我将发布更新的代码。