views:

950

answers:

2

We use the code below to inject Spring beans with properties from a properties file.

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:/my.properties"/>
</bean>

<bean id="blah" class="abc">
    <property name="path" value="${the.path}"/>
</bean>

Is there a way we can access the properties programatically? I'm trying to do some code without dependency injection. So I'd like to just have some code like this:

PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
props.load("classpath:/my.properties");
props.get("path");
+2  A: 

I have done this and it has worked.

Properties props = PropertiesLoaderUtils.loadAllProperties("my.properties");
PropertyPlaceholderConfigurer props2 = new PropertyPlaceholderConfigurer();
props2.setProperties(props);

That should work.

Zoidberg
+6  A: 

How about PropertiesLoaderUtils?

Resource resource = new ClasspathResource("/my.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
skaffman
here is a question, how is this different from mine, and has two more votes AND posted second...
Zoidberg
Beats me, I didn't get to vote :) I wouldn't use a `PropertyPlaceholderConfigurer`, though it's overkill for the task.
skaffman
I was trying to get as close to what he had as possible, I have been downvoted so many times for not providing enough detail. In anycase, your answers deserve the votes, as it is correct, I guess I am just jealous I didn't get 2 votes as well, LOL.
Zoidberg