This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

$ expressions in applicationContext.xml not evaluated

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this.
The value is generated at compile time but it's available at runtime definitely.
 
Priyanka Sumanam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...thanks for explaining...possible that I got it wrong somewhere....will let you know if I have any other ideas.
 
Priyanka Sumanam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saw this some where.......may be this could help.....am not sure


Before (Spring 2.0 and earlier):



After (Spring 2.5):

 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
Maciej Opala
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seriously Rick? Seriously? You might as well just read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic