Solved

Spring : Unable to read from a properties file

Posted on 2014-02-04
7
776 Views
Last Modified: 2014-02-11
I have a bean defined

	<bean id="placeholderConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:project.properties</value>
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true"/>
	</bean>


    <bean id="processor" class="com.util.Processor">    	 
    	<property name="system" value="${system}"/>
    </bean>

Open in new window


In project.properties i have
system = TECH-UK

Open in new window


My Processor

public class Processor implements BeanDefinitionRegistryPostProcessor
{
private String system;

	public void setSystem(String system) {
		this.system = system;
	}


	public String getSystem() {
		return system;
	}

@Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

  //create beans
}

Open in new window


When i put a break point on this line >>this.system = system;
I expect to see value of system as TECH-UK but i see ${system}

I think the problem is with implementing BeanDefinitionRegistryPostProcessor
When i comment out the  'implements BeanDefinitionRegistryPostProcessor' i see the value TECH-UK

Any idea how i can fix this.

Thanks
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 39834185
I think the problem is with implementing BeanDefinitionRegistryPostProcessor
Spot on! The reason why it doesn't replace the ${system} with its value is because PropertyPlaceholderConfigurer is implemented as a BeanFactoryPostProcessor too. And when Spring is initializing an ApplicationContext it instantiates ALL BeanFactoryPostProcessors before any of them get invoked. So by the time that the PropertyPlaceholderConfigurer gets a chance to replace the ${system} with TECH-UK, your Processor has already been instantiated and the setSystem() method has already been called with ${system} as the argument.

More correctly, since you implement BeanDefinitionRegistryPostProcessor, these actually get instantiated and invoked before even regular BeanFactoryPostProcessors so there is definitely no chance to get the placeholder resolved.

Unfortunately, there is no way around this, basically BeanFactoryPostProcessors (and the sub-interface BeanDefinitionRegistryPostProcessor) don't mix well with PropertyPlaceholderConfigurer. You would need to do something quite different to what you have above, such as... inject the location of the .properties file directly into your Processor and have it load it into a Properties object and then resolve the value of the "system" property, etc.
0
 

Author Comment

by:royjayd
ID: 39834399
>>You would need to do something quite different to what you have above, such as.. inject the location of the .properties file directly into your processor and have it load into a Properties object...

Lets say I create a Properties object which contains the entires present in my properties file,
how do I set it to BeanDefinitionRegistryPostProcessor ?

Can you please provide some sample code or link if available..

Thanks for the prompt response.
0
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 39834443
I was thinking more like this...
public class Processor implements BeanDefinitionRegistryPostProcessor
{
	private String system;

	public void setPropertiesLocation(Resource location) {
		Properties props = new Properties();
		props.load(location.getInputStream());
		this.system = props.getProperty("system");
	}

@Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

  //create beans
}

Open in new window

And then your spring config just looks like this...
    <bean id="processor" class="com.util.Processor">    	 
    	<property name="propertiesLocation" value="classpath:project.properties"/>
    </bean>

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:royjayd
ID: 39848882
Thanks..
Any help with my next question will be greatly appreciated.
http://www.experts-exchange.com/Programming/Languages/Java/Q_28361464.html
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39849064
No worries, glad to help! :)
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39849070
Any help with my next question will be greatly appreciated.
Sorry, I have 0 experience with Drools! Hope someone else can help you.
0
 

Author Comment

by:royjayd
ID: 39849916
No worries :)
Thx.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question