Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting class level annotation values from application.yml

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 ?

like image 699
Shak Avatar asked Jul 25 '26 10:07

Shak


1 Answers

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.

like image 50
Iulian Rosca Avatar answered Jul 27 '26 03:07

Iulian Rosca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!