从环境变量设置注释属性?
set an annotation attribute from an environment variable?
我正在尝试从环境变量设置注释值:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableScheduling
class Application {
@Scheduled(cron = "${DB_CRON}")
def void schedule() {
...
}
public static void main(String... args) {
SpringApplication.run(Application, args)
}
...
}
但是,我得到以下编译时错误:
Attribute 'cron' should have type 'java.lang.String'; but found type
'java.lang.Object' in
@org.springframework.scheduling.annotation.Scheduled
是否可以这样设置注释,或者我是否需要使用其他技术,例如在 属性 文件中设置值?
@Scheduled(cron = "#{systemEnvironment['ANDROID_HOME']}")
def void schedule() {
...
}
您不能在 groovy 的 java 注释中使用 GString。您必须使用 "proper" 字符串。例如
@Scheduled(cron = '${DB_CRON}')
注意这里的单引号。如果 groovy 在 "
引号字符串中看到 $
,它将把它变成一个 GString。这不能用 java 注释来完成,你实际上不想在这里做,因为你想在这里设置你的 spring 属性。这也是错误消息在这里试图说明的,这里没有使用基本类型字符串,而是使用了一些对象(GString)。
我正在尝试从环境变量设置注释值:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableScheduling
class Application {
@Scheduled(cron = "${DB_CRON}")
def void schedule() {
...
}
public static void main(String... args) {
SpringApplication.run(Application, args)
}
...
}
但是,我得到以下编译时错误:
Attribute 'cron' should have type 'java.lang.String'; but found type 'java.lang.Object' in @org.springframework.scheduling.annotation.Scheduled
是否可以这样设置注释,或者我是否需要使用其他技术,例如在 属性 文件中设置值?
@Scheduled(cron = "#{systemEnvironment['ANDROID_HOME']}")
def void schedule() {
...
}
您不能在 groovy 的 java 注释中使用 GString。您必须使用 "proper" 字符串。例如
@Scheduled(cron = '${DB_CRON}')
注意这里的单引号。如果 groovy 在 "
引号字符串中看到 $
,它将把它变成一个 GString。这不能用 java 注释来完成,你实际上不想在这里做,因为你想在这里设置你的 spring 属性。这也是错误消息在这里试图说明的,这里没有使用基本类型字符串,而是使用了一些对象(GString)。