Mikhail  Tsaplin
Mikhail Tsaplin

Reputation: 642

Obtaining configured properties from Springs

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.

Upvotes: 0

Views: 87

Answers (1)

geoand
geoand

Reputation: 63991

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

Upvotes: 1

Related Questions