英文:
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 = "${my.cron.expression}")
public void test(){
log.info("hello!");
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论