Cron 表达式必须包含 6 个字段(在“${cron.expression} 中找到 1 个)部分已修复但 AnnotationConfigApplicationContext 存在问题

Cron expression must consist of 6 fields(found 1 in "${cron.expression}) partially fixed but having issues with AnnotationConfigApplicationContext

我正在尝试参数化 cron 表达式并从属性文件中读取它。在此过程中,我得到以下异常 "Error creating bean with name 'springScheduleCronExample': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'cronJob': Cron expression must consist of 6 fields (found 1 in "${cron.expression}")"。

然后我发现了以下post

使用我正在读取的 cron 表达式,只有当我有

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( SpringScheduleCronExample.class);

在我的主要方法中定义。我遇到的问题是,我想 运行 在没有主要方法的服务器上执行此操作,任何人都可以帮我解决这个问题。

这是我的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>

我的 SpringScheduleCronExample.java 看起来像这样

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.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
@PropertySource("classpath:application.properties")
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=*/5 * * * * ?

这是我如何让它工作的

申请-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">


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

MyApplicationConfig.java

package com.hemal.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
@ComponentScan(basePackages = {"com.hemal.spring"})
@PropertySource("classpath:application.properties")
public class MyApplicationConfig {

    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();

        properties.setLocation(new ClassPathResource( "application.properties" ));
        properties.setIgnoreResourceNotFound(false);

        return properties;
    }
}

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));
    }
}

我的调度程序class

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();
        }
    }
}

application.properties cron.expression=0/5 * * * * ?