File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes Context Properties and Bean Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Context Properties and Bean Initialization" Watch "Context Properties and Bean Initialization" New topic

Context Properties and Bean Initialization

Db Riekhof

Joined: May 21, 2009
Posts: 10

I'm using Spring 3.1 and Tomcat 7.0.25. I'd like to get some custom property files loaded before the Spring Container processes my applicationContext.xml and initializes some beans in there. So after some research I found that a nice new way to do something like this in 3.1 is to use a custom ApplicationContextInitializer class. I tied it in my web.xml like this:

Then I wrote my class here:

It all works fine except for one Major problem. Beans inside my applicationContext.xml are getting initalized before my ContextPropsLoader code is getting executed. I know this by the errors it is showing, and by running it in the debugger and seeing the errors before it gets to the ContextPropsLoader code:

The db.driver.class.postgresql property is defined in my ContextPropsLoader, but too late after bean initialization.

Suggestions? Anyway to get that code to run sooner or the beans to initialize later?

Thanks for reading.
Manish Sridharan
Ranch Hand

Joined: Jul 19, 2005
Posts: 64
Have you added ContextLoaderListener in your web.xml ?


Manish S.
Db Riekhof

Joined: May 21, 2009
Posts: 10
Manish Sridharan wrote:Have you added ContextLoaderListener in your web.xml ?


Yes, I had the listener defined just as you say in web.xml. I moved it up to the top, and it still doesn't matter, same error.
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Why don't you use PropertyPlaceHolderConfigurer ?
I agree. Here's the link:
subject: Context Properties and Bean Initialization
Similar Threads
BeanCreationException in an standalone application using Spring
Web application
Problems with Spring setup in tomcat
Exception in thread "main" org.springframework.beans.factory.BeanCreationException
Getting Error in setting property values