java.lang.IllegalStateException:遇到无效的@Scheduled 方法:无法解析占位符@PropertySource("classpath:dev.yml")

java.lang.IllegalStateException: Encountered invalid @Scheduled method: Could not resolve placeholder @PropertySource("classpath:dev.yml")

我正在尝试加载以下 cron-execution-expression:

---
####################### Cron-Job Every 2mins every day #######################
cron:
  exe:
    expression: 0 0/2 * * * ?

要注意的是,上面的 cron-expression 在 Spring-Cloud-Config 中(假设 Springboot 项目 A,运行 端口:8001)github 存储库。命名为:微服务-dev.yml

项目 B(端口:8002)在启动时加载了项目 A 提供的所有配置,对此我很满意。但是我如何找到这个表达式呢?

${cron.exe.expression}

@Component
//Couldn't get it to work with Spring-Cloud-Config
@PropertySource("classpath:microservice-dev.yml")
public class MergeCachedRecordsToDBImpl {
    private static final Logger LOGGER = LoggerFactory.getLogger(MergeCachedRecordsToDBImpl.class);

    //Couldn't get it to work with Spring-Cloud Config
    @Scheduled(cron = "${cron.exe.expression}")
    public void purgeExpired() {
        LOGGER.info("Cron-Job Notification....");
        LOGGER.info("Cron-Job executed at: {}", new Timestamp(new Date().getTime()));
    }
}

在某些时候,我让它工作了,但我不确定怎么做?我正在努力追溯我的脚步。

现在我遇到了这个异常:

Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'purgeExpired': Could not resolve placeholder 'cron.exe.expression' in value "${cron.exe.expression}"

目前 spring 启动 @PropertySource 不支持 yaml 作为 属性 文件。

这是类似的问题 -

好的,让它再次使用 .yml 文件。快乐的日子。

在以下 .yml 文件中:

src/main/resources/bootstrap.yml

有以下内容:

---
spring:
  application:
    name: leaderboard
  profiles:
    active: dev

server:
  port: 8004

---
####################### Cron-Job Every 2mins every day #######################
cron:
  exe:
    expression: 0 0/2 * * * ?

在代码中,我用以下注释注释了我的 class 和方法:

@PropertySource("classpath:bootstrap.yml")
public class CronClass{
private static final Logger LOGGER = LoggerFactory.getLogger(CronClass.class);

   @Scheduled(cron = "${cron.exe.expression}")
   private void cornJob(){
        LOGGER.info("Cron-Job Notification....");
        LOGGER.info("Cron-Job executed at: {}", new Timestamp(new Date().getTime()));
   }
}

结果:

2018-09-29 11:19:00.007  INFO [leaderboard,66037f3d8052cf6b,66037f3d8052cf6b,false] 7937 --- [   scheduling-1] i.s.l.task.CronClass    : Cron-Job Notification....
2018-09-29 11:19:00.007  INFO [leaderboard,66037f3d8052cf6b,66037f3d8052cf6b,false] 7937 --- [   scheduling-1] i.s.l.task.CronClass    : Cron-Job executed at: 2018-09-29 11:19:00.007

2018-09-29 11:20:00.000  INFO [leaderboard,25cab214549b76a6,25cab214549b76a6,false] 7937 --- [   scheduling-1] i.s.l.task.CronClass    : Cron-Job Notification....
2018-09-29 11:20:00.000  INFO [leaderboard,25cab214549b76a6,25cab214549b76a6,false] 7937 --- [   scheduling-1] i.s.l.task.CronClass    : Cron-Job executed at: 2018-09-29 11:20:00.0

2018-09-29 11:21:00.000  INFO [leaderboard,c2f241d8a806fd26,c2f241d8a806fd26,false] 7937 --- [   scheduling-1] i.s.l.task.CronClass    : Cron-Job Notification....
2018-09-29 11:21:00.000  INFO [leaderboard,c2f241d8a806fd26,c2f241d8a806fd26,false] 7937 --- [   scheduling-1] i.s.l.task.CronClass    : Cron-Job executed at: 2018-09-29 11:21:00.0