使用参数与Spring的@Scheduled表达式

huangapple go评论136阅读模式
英文:

Using parameters with a spring @Scheduled expression

问题

I'm trying to paramaterize a cron expression currently used for a Spring @Scheduled expression.
All other variables coming from the client configuration seem to be loaded correctly for other variables, but Spring doesn't seem to be able to pick up anything in the cron expression.

I've tried a variety of different ways of doing it but none seem to work. Throwing errors like

I've tried calling the expression directly from the config file

@Scheduled(cron = "${my.cron.expression}")
public void importData() {

using the @Qualifier and giving it a direct string name

@Qualifier("cronExpression") String cronExpression
this.cronExpression = cronExpression
...
@Scheduled(cron = "${cronExpression}")
public void importData() {

but these all still return the above error message

In the client config class, I'm currently trying to import the variable like this:

@Bean
@Qualifier("cronExpression")
public String cronExpression(@Value("${my.cron.expression}") String cronExpression) {
    return cronExpression;
}

Aside from names, this is the code I'm using which is still throwing an unresolved placeholder error:
Properties:

my:
  cron:
    expression: 0 0 4-6 * * *

ClientConfig:

@Bean
@Qualifier("cronExpression")
public String cronExpression(@Value("${my.cron.expression}") String cronExpression) {
    return cronExpression;
}

Scheduler:

private final String cronExpression;
...
@Qualifier("cronExpression") String cronExpression
...
this.cronExpression = cronExpression
...
@Scheduled(cron = "${cronExpression}")
英文:

I'm trying to paramaterize a cron expression currently used for a Spring @Scheduled expression.
All other variables coming from the client configuration seem to be loaded correctly for other variables, but Spring doesn't seem to be able to pick up anything in the cron expression.

I've tried a variety of different ways of doing it but none seem to work. Throwing errors like

java.lang.IllegalStateException: Encountered invalid @Scheduled method 'importData': Could not resolve placeholder 'cronExpression' in value "${cronExpression}"

I've tried calling the expression directly from the config file

@Scheduled(cron = "${my.cron.expression}")
    public void importData() {

using the @Qualifier and giving it a direct string name

@Qualifier("cronExpression") String cronExpression
this.cronExpression = cronExpression
...
@Scheduled(cron = "${cronExpression}")
    public void importData() {

but these all still return the above error message

In the client config class, I'm currently trying to import the variable like this:

    @Bean
    @Qualifier("cronExpression")
    public String cronExpression(@Value("${my.cron.expression}") String cronExpression) {
        return cronExpression;
    }

Aside from names, this is the code I'm using which is still throwing an unresolved placeholder error:
Properties:

my:
  cron:
    expression: 0 0 4-6 * * *

ClientConfig:

    @Qualifier("cronExpression")
    public String cronExpression(@Value("${my.cron.expression}") String cronExpression) {
        return cronExpression;
    }

Scheduler:

private final String cronExpression;
...
@Qualifier("cronExpression") String cronExpression
...
this.cronExpression = cronExpression
...
@Scheduled(cron = "${cronExpression}")


</details>


# 答案1
**得分**: 1

以下是我的Spring Java类:

```java
@EnableScheduling
@SpringBootApplication
@Slf4j
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Scheduled(cron = "${my.cron.expression}")
    public void test(){
        log.info("hello!");
    }
}

这是我的application.yml配置文件:

my.cron.expression: 0 */1 * * * ?

使用这个配置,我的日志每分钟触发一次。
(我还没有添加特定的依赖项)


注意

请注意你的属性的字符串包装方式:如果Spring报错,请尝试取消引用

英文:

Here's my Spring Java class:

<!-- language: lang-java -->

@EnableScheduling
@SpringBootApplication
@Slf4j
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Scheduled(cron = &quot;${my.cron.expression}&quot;)
	public void test(){
		log.info(&quot;hello!&quot;);
	}
}

And here's my application.yml configuration file:

<!-- language: lang-yaml -->

my.cron.expression: 0 */1 * * * ?

With this configuration, my log is triggered every minute.
(I haven't added particular dependencies)


NOTE

Be careful with the String wrapping of your property: try to unquote it, if Spring gives you an error.

huangapple
  • 本文由 发表于 2020年8月6日 00:00:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63269075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定