0

I am trying to obtain properties' values configured in Springs context with Environment bean (like in spring PropertyPlaceholderConfigurer and context:property-placeholder checked answer).

public class SpringsPropertiesProvider implements IPropertiesProvider {
    @Autowired Environment envinronment;

    @Override
    public String getProperty(String key) {
        return envinronment.getProperty(key);
    }

}

This class is registered with following xml:

<context:property-placeholder
    location="classpath:myproject/example.properties" />
<context:annotation-config />
<bean class="myproject.SpringsPropertiesProvider" id="springsPropertiesProvider"/>

But SpringsPropertiesProvider.getProperty method does not return values configured within example.properties file.

What I am doing wrong and how can I get dynamic access to properties configured by placeholderconfigurer?

PS. During environment.getPropert(key) call debugging shows that org.springframework.core.env.PropertySourcesPropertyResolver has only two entries in its propertySources field ([systemProperties,systemEnvironment]) and both entries does not contain any keys defined within example.properties.

Community
  • 1
  • 1
Mikhail Tsaplin
  • 642
  • 1
  • 9
  • 21

1 Answers1

1

Try this

<context:property-placeholder
    location="classpath:myproject/example.properties" ignore-resource-not-found="true"/>

If the project does not startup then that means spring was not able to locate the properties file. Speaking of which, what does your project structure look like?

Update:

The following link explains why this is not working

Community
  • 1
  • 1
geoand
  • 60,071
  • 24
  • 172
  • 190
  • Geoand thank you. I checked this almost as you suggested - by making file path incorrect and context loading consequently was failed - so original path setting was correct. – Mikhail Tsaplin Mar 12 '14 at 07:18
  • What happens if you inject a property with @Value("${nameOfProperty})? – geoand Mar 12 '14 at 07:22
  • It seems that the Environment objects does not contain user specified properties. Take a look here: http://stackoverflow.com/a/21106326/2504224 – geoand Mar 12 '14 at 07:34
  • Seems that the answer in this link explains why it is not working. But I still need to find the way to get configured properties without direct passing references to props. files. Please edit your answer with reference to this link. – Mikhail Tsaplin Mar 12 '14 at 07:57