Setting class level annotation values from applica

2020-05-06 04:52发布

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 {

   } 

And application.yml

cyclone:
  hystrix:
    lease: 
      thread:
        coreSize: 10

Getting an error --

java.lang.IllegalArgumentException: bad property value. property name 'coreSize'. Expected int value, actual = ${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 ?

1条回答
等我变得足够好
2楼-- · 2020-05-06 05:19

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();
}

According to the JavaDoc:

Specialization of PlaceholderConfigurerSupport that resolves ${...} placeholders within bean definition property values and @Value annotations against the current Spring Environment and its set of PropertySources.

查看更多
登录 后发表回答