Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Value not resolved when using @PropertySource annotation. How to configure PropertySourcesPlaceholderConfigurer?

I have following configuration class:

@Configuration @PropertySource(name = "props", value = "classpath:/app-config.properties") @ComponentScan("service") public class AppConfig { 

and I have service with property:

@Component  public class SomeService {     @Value("#{props['some.property']}") private String someProperty; 

I receive error when I want to test the AppConfig configuration class with

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String service.SomeService.someProperty; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'props' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'  

The issue is documented in SPR-8539

but anyway I cannot figure out how to configure PropertySourcesPlaceholderConfigurer to get it work.

Edit 1

This approach works well with xml configuration

<util:properties id="props" location="classpath:/app-config.properties" /> 

but I want to use java for configuration.

like image 366
matus Avatar asked Dec 05 '12 16:12

matus


1 Answers

as @cwash said;

@Configuration @PropertySource("classpath:/test-config.properties") public class TestConfig {       @Value("${name}")      public String name;        //You need this      @Bean      public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {         return new PropertySourcesPlaceholderConfigurer();      }  } 
like image 56
baybora.oren Avatar answered Sep 17 '22 18:09

baybora.oren