aspose file tools*
The moose likes Spring and the fly likes $ expressions in applicationContext.xml not evaluated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "$ expressions in applicationContext.xml not evaluated" Watch "$ expressions in applicationContext.xml not evaluated" New topic
Author

$ expressions in applicationContext.xml not evaluated

Maciej Opala
Ranch Hand

Joined: Jul 18, 2011
Posts: 38
Hi,
I've the following problem (and for the whole day I haven't found any solution): the $"" expressions in my app's applicationContext.xml aren't evaluated.

Here are the sources:

1) applicationContext.xml


2) web.xml


3) db.properties


4) org.opal.test.spring.props.CustomPropertiesLoader


When I deploy the war to the server (jetty, jboss) everything is fine, the applicationContext.xml is processed, the db.properties file is found, but when the execution goes to the CustomPropertyLoader class I get ClassNotFoundException: "${db.driver}". Everything works fine when I put the db details from the properties into applicationContext.xml directly, otherwise the mentioned exception occurs.
I use Spring version 2.5.6 with maven.
Any help would be greatly appreciated because this problem is really annoying.
Priyanka Sumanam
Greenhorn

Joined: Aug 17, 2010
Posts: 25
have you configured your db.properties file in pom.xml as below




for your referencce
http://maven.apache.org/pom.html#Properties

Please ignore if you have already don it.
Maciej Opala
Ranch Hand

Joined: Jul 18, 2011
Posts: 38
No, will check it. Thank You.

But to be honest I don't think this is the problem or maybe there's a misunderstanding. These properties are to be evaluated at runtime or inserted at compile time?
Priyanka Sumanam
Greenhorn

Joined: Aug 17, 2010
Posts: 25
Irrespective of, if its build time or runtime thing, there should be a way to tell maven to add those resources to be available for the project, to go and search for the ${name} values to be replaced by its values.

And ofcourse it is a buildtime thng.When you you bild you project maven replaces all ${name} properties wiht its values if they are found in the properties files that it is aware of.
If we dont configure them in pom, they go unnoticed.
Maciej Opala
Ranch Hand

Joined: Jul 18, 2011
Posts: 38
Ok, but if it's compile time task it doesn't make any sense to tell PropertyPlaceholderConfigurer to search db.properties in classpath to be honest. I doubt it this is compile time thing.
Maciej Opala
Ranch Hand

Joined: Jul 18, 2011
Posts: 38
Have a look at this.
The value is generated at compile time but it's available at runtime definitely.
Priyanka Sumanam
Greenhorn

Joined: Aug 17, 2010
Posts: 25
If you set all you properties under <properties> tag in pom you will not get this problem as per the link you attached.But i didn't suggest you this a
its not feasable to lis all the properties in pom when you have many.
But both are same.It is way to let maven know about the properties you want to use acorss the application.

coming back to you question, i gues you exception is raised exactly at this lin



I am not sure what the DatabaseConfiguration constructor is doing internally .But it is trying to acces the Datasource bean which is wired in you application context as



now it is trying to get the driverClassName name.But here ${db.driver} is not replaced by the actual value at build time as it did not find the property and so it is trying to search for a driverclass with name ${db.driver} which is obviously not found.

And even if it happens at runtime , maven should know where the db.properties file is to replace the valuse. if you configure your properties under properties tag in pom it will create a build.properties for all the ones there.But if you are using you own , you should tell it so.

Any way did you try configuring it as i suggersted ealier?

Maciej Opala
Ranch Hand

Joined: Jul 18, 2011
Posts: 38
No, I didn't cause it has nothing to do with maven processing.

The problem is the fact that two PropertyPlaceholderConfigurer (the class CustomPropertyLoader is also a PropertyPlaceholderConfigurer) beans are registered and they do disturb each other with resolving the expressions correctly. I don't know exactly how to handle this despite I've already tried to use order, prefix and suffix properties.

It's works perfectly when only one PropertyPlaceholderConfigurer is registered, and I haven't "told maven" where the property file is - it's the spring job to find it and spring does this job well. If it doesn't the exception is FileNotFoundException (tested) instead of ClassNotFoundException.

Also please mind that I'm going to give a built archive (war) to somebody and tell this person just to change the db.properties file to his/her own set of properties. This person isn't going to build the application with maven once again, so as I said maven has nothing to do with this problem. This is only spring case.
Priyanka Sumanam
Greenhorn

Joined: Aug 17, 2010
Posts: 25
Ok...thanks for explaining...possible that I got it wrong somewhere....will let you know if I have any other ideas.
Priyanka Sumanam
Greenhorn

Joined: Aug 17, 2010
Posts: 25
Saw this some where.......may be this could help.....am not sure


Before (Spring 2.0 and earlier):



After (Spring 2.5):

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Priyanka Sumanam wrote:Saw this some where.......may be this could help.....am not sure


Before (Spring 2.0 and earlier):



After (Spring 2.5):



What that means is that in Spring 2.5 Spring introduced the property-placeholder tag into the context namespace. But those two things you wrote mean exactly the same thing. One is just less typing to do. But they are identical in results.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
The exception telling its cannot find the class file it try to load so, there are several checks that you need to make sure they are correct.

1. The location of db.properties file, based on the "classpath:" setting look like
you put the db.properties file under "src/main" directory in your webapp path.



If you place db.properties somewhere else under "/WEB-INF/" you need to specify it exact location, otherwise, it won't find your file.


2. Make sure your "DatabaseConfiguration" is correctly configured






DatabaseConfiguration from the source: http://commons.apache.org/configuration/apidocs/org/apache/commons/configuration/DatabaseConfiguration.html



Build a configuration from a table.-

Parameters:
datasource - the datasource to connect to the database
table - the name of the table containing the configurations
keyColumn - the column containing the keys of the configuration
valueColumn - the column containing the values of the configuration

3. Make sure your db.properties file properly configured

For example,



Make sure your port and the setting is correct relative to your application and configuration.


4. Maybe something else not right if all above suggestion is met.





OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Maciej Opala
Ranch Hand

Joined: Jul 18, 2011
Posts: 38
Thanks for the respone. Actually what was happening is well described here. I've solved the problem in Spring 3 with no XML configuration. If anybody is interested just write.
 
 
subject: $ expressions in applicationContext.xml not evaluated