Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Spring : Unable to read from a properties file

Posted on 2014-02-04
7
Medium Priority
?
825 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 2000 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 …
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

670 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