@Value is not allowed when using the @PropertySource annotation. How to set PropertySourcesPlaceholderConfigurer?

I have the following configuration class:

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

and I have a service with the property:

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

I get an 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 problem is documented in SPR-8539

but anyway, I can't figure out how to set PropertySourcesPlaceholderConfigurer to make it work.

Change 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.

+47
spring configuration
Dec 05
source share
8 answers

If you are using @PropertySource, the properties must be obtained using:

 @Autowired Environment env; // ... String subject = env.getProperty("mail.subject"); 

If you want to get using @Value ("$ {mail.subject}"), you need to register the xml location of the firmware.

Reason: https://jira.springsource.org/browse/SPR-8539

+25
Mar 22 '13 at 3:38 on
source share

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(); } } 
+103
Sep 12 '13 at 7:41
source share

I found that the reason @value does not work for me, @value requires PropertySourcesPlaceholderConfigurer instead of PropertyPlaceholderConfigurer . I made the same changes and it worked for me, I am using spring 4.0.3 release. I configured this using the code below in my configuration file.

 @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } 
+13
Nov 27 '14 at 6:56
source share

You don't need a method in the @Configuration class that returns a PropertySourcesPlaceholderConfigurer annotated by @ Bean and is static to register any @PropertySource with Spring?

http://www.baeldung.com/2012/02/06/properties-with-spring/#java

https://jira.springsource.org/browse/SPR-8539

+10
Jun 10 '13 at 19:21
source share

I had the same problem. @PropertySource does not work with @Value . A quick workaround is to have an XML configuration that you will reference from your Spring Java configuration using @ImportResource , as usual, and that the XML configuration file will contain one entry: <context:property-placeholder /> ( of course, with the necessary namespace ceremony). Without any other changes, @Value will inject properties into your @Configuration pojo.

+5
May 27 '13 at 12:14
source share

It can also be configured in java this way

 @Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setIgnoreUnresolvablePlaceholders(true); configurer.setIgnoreResourceNotFound(true); return configurer; } 
+5
Apr 21 '16 at 20:17
source share

It looks very complicated, can't you just do

  <context:property-placeholder location="classpath:some.properties" ignore-unresolvable="true"/> 

then in the link to the code:

 @Value("${myProperty}") private String myString; @Value("${myProperty.two}") private String myStringTwo; 

where some.properties looks something like this:

 myProperty = whatever myProperty.two = something else\ that consists of multiline string 

For java configuration you can do this

 @Configuration @PropertySource(value="classpath:some.properties") public class SomeService { 

And then just type @value as before

+2
Dec 05
source share

The thing is, as I understand it, <util: propertes id = "id" location = "loc" /> is just a shorthand for

 <bean id="id" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="loc"/> </bean> 

(see utility documentation: properties ). Thus, using the util: properties creates a standalone bean.

@PropertySource, on the other hand, as the documentation says,

An annotation providing a convenient and declarative mechanism for adding a PropertySource property in Spring Environment '.

(see @PropertySource doc ). Therefore, he does not create any bean.

Then "# {a ['something']}" is a SpEL expression (see SpEL ), which means "get something from bean 'a'". When used: properties are used, a bean exists, and the expression makes sense, but when @PropertySource is used, there is no actual bean, and the expression is meaningless.

You can get around this either with XML (which is best, I think), or by creating a PropertiesFactoryBean property, declaring it as a regular @ Bean.

+1
May 31 '13 at 9:40
source share



All Articles