Solved

Spring : Unable to read from a properties file

Posted on 2014-02-04
7
682 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
  • 4
  • 3
7 Comments
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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
Comment Utility
>>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 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

Expert Comment

by:mccarl
Comment Utility
No worries, glad to help! :)
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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
Comment Utility
No worries :)
Thx.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now