Solved

Spring : Unable to read from a properties file

Posted on 2014-02-04
7
703 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to start eclipse ? 17 133
looking for unix program to search some keywords in side EAR WAR and JAR files 10 82
maven example error 3 53
servlet example 17 10
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 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

21 Experts available now in Live!

Get 1:1 Help Now