Solved

Spring : Unable to read from a properties file

Posted on 2014-02-04
7
732 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
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 35

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 35

Expert Comment

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 56
tomcat administrtor 12 61
ejb stateless example 2 25
ejb entity bean example 2 15
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

860 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