我在一个遗留软件中工作,其中大部分配置是从application.properties它外部化的,它驻留在一个名为的文件中,该文件custom.properties将被读入一个这样声明的配置 bean。
@Configuration
@ConfigurationProperties(locations = "classpath:custom.properties", ignoreUnknownFields = true, prefix = "custom")
public class CustomProperties {
...
}
Run Code Online (Sandbox Code Playgroud)
此应用程序有一些计划任务,它们声明以固定的时间间隔和时间工作。@Scheduled(cron = "0 0 16 * * 3")到目前为止,一切正常。最近,我被要求在可配置的时间执行此 cronjob。所以我添加了另一个属性custom.properties和一个属性CustomProperties(包括 getter 和 setter)。接下来,我将预定的注释更改为如下所示。@Scheduled(cron = "${@customProperties.cronJob1Schedule}")
当我启动应用程序时,出现以下异常:
java.lang.IllegalStateException: Encountered invalid @Scheduled method 'cronJob1': Could not resolve placeholder '@customProperties.cronJob1Schedule' in string value "${@bwvProperties.cronJob1Schedule}"
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:406)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:282)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
Run Code Online (Sandbox Code Playgroud)
以前有人遇到过这个问题吗?为什么我无法访问 SpEL 中的配置 bean?
请注意,有两种不同的机制可以处理 Spring 处理的字符串值:
${my.property}Spring 能够${placeholder}用来自配置的属性源的值替换占位符。这个过程只是用一个字符串值替换字符串键(如果需要,它会通过转换器运行)。
#{spel.expression}Spring 能够#{}通过SPeL解释器运行内容。这提供了更强大的工具,因为您可以从表达式内部与应用程序代码进行交互,例如通过从您的 bean 之一获取属性#{@cumstomProperties.cronJob1Schedule}。
你只需要切换${为#{您的注释值。
| 归档时间: |
|
| 查看次数: |
4965 次 |
| 最近记录: |