从application.yml设置类级别的注释值 [英] Setting class level annotation values from application.yml
问题描述
我试图使用allication.yaml来外部化类级别注释的配置.但是春天并没有加载正确的时间.任何想法如何做到这一点?
I was trying to externalise my configuration of a class level annotation using allication.yaml. But the spring is not loading it right. Any idea how to do this ?
这是我正在设置的服务类别
Here is my Service classI am trying to set
@Service
@DefaultProperties(threadPoolProperties = {
@HystrixProperty(name = "coreSize", value =
"${cyclone.hystrix.lease.thread.coreSize}") })
public class LeaseService {
}
和application.yml
And application.yml
cyclone:
hystrix:
lease:
thread:
coreSize: 10
遇到错误-
java.lang.IllegalArgumentException: bad property value. property name 'coreSize'. Expected int value, actual = ${cyclone.hystrix.lease.thread.coreSize}
我可以使用@Value("$ {cyclone.hystrix.lease.thread.coreSize}")加载相同的属性.但无法处理上述情况. 任何有关如何正确配置此配置的帮助?
I can load the same property using @Value("${cyclone.hystrix.lease.thread.coreSize}"). But not working on the above mentioned case. Any help on how to properly configure this ?
推荐答案
为了使spring评估占位符,在使用@Configuration
类时,需要使用静态@Bean
方法注册PropertySourcesPlaceholderConfigurer
bean,如下所示:
In order to make spring evaluate placeholders you need to register a PropertySourcesPlaceholderConfigurer
bean using a static @Bean
method when using @Configuration
classes as follows:
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
根据 JavaDoc :
PlaceholderConfigurerSupport的专业化,可针对当前Spring Environment及其PropertySources集合解析Bean定义属性值和@Value注释内的$ {...}占位符.
Specialization of PlaceholderConfigurerSupport that resolves ${...} placeholders within bean definition property values and @Value annotations against the current Spring Environment and its set of PropertySources.
这篇关于从application.yml设置类级别的注释值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!