Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Web

Reply
 
Thread Tools Display Modes
  #1  
Old Nov 9th, 2004, 11:46 AM
rosco rosco is offline
Junior Member
 
Join Date: Sep 2004
Posts: 18
Default Using PropertyPlaceholderConfigurer in servlet.xml

How can I use a PropertyPlaceholderConfigurer in the *-servlet.xml files? I want to replace certain controller implementation class names using the same properties file that the applicationContext.xml uses.

Can someone please advise?

Thanks
Reply With Quote
  #2  
Old Nov 9th, 2004, 12:20 PM
ojolly ojolly is offline
Senior Member
 
Join Date: Aug 2004
Location: Toulouse, France
Posts: 148
Default

In general, when you want to use a BeanFactoryPostProcessor in an ApplicationContext, you simply have to declare a bean of this type and it will be interpreted by the application context. The same applies to configuration of servlet, being a XmlWebApplicationContext.
However, you seems to be willing to change the class name. Afaik this is not interpreted by post processor, which can only change property content. You can have a try but I think it doesn't work.
In this case, you could imagine having beans which are in fact FactoryBean implementation and are configured with a property indicating the class name of the bean to create. Have a look at FactoryBean interface, it is really easy.

HTH

Olivier
Reply With Quote
  #3  
Old Nov 9th, 2004, 01:02 PM
rosco rosco is offline
Junior Member
 
Join Date: Sep 2004
Posts: 18
Default

Its not just the class name that I want to configure. I also have string properties on many of my controllers that I want to externalize into a .properties file using the PropertyPlaceholderConfigurer and the ${...} syntax. It works great in the applicationContext.xml file but it doesn't seem to work in the *-servlet.xml files.
Reply With Quote
  #4  
Old Nov 9th, 2004, 01:40 PM
ojolly ojolly is offline
Senior Member
 
Join Date: Aug 2004
Location: Toulouse, France
Posts: 148
Default

There's nothing which could explain why it works in applicationContext-xml and not *-servlet.xml. Can you turn on log on level debug and dump the result ?
By "not working", I assume the properties are valued to "${....}" instead of the property value and that you don't have exception, right ?

Olivier
Reply With Quote
  #5  
Old Nov 9th, 2004, 01:52 PM
rosco rosco is offline
Junior Member
 
Join Date: Sep 2004
Posts: 18
Default

Olivier,

Thanks for your help. I spoke too soon, it does replace values correctly, but not the class="" attribute of a bean definition. You were 100% correct in your previous post.

My course of action at this point is to define 2 controllers and use the property placeholder to point my url mapping to the correct controller:


<prop key="/entry.html">${entry.controller}</prop>

Then in the .properties file I will have either:

entry.controller=devEntryController OR entry.controller=prodEntryController

depending on my build script.

Thanks again for your help.
Reply With Quote
  #6  
Old Jan 20th, 2005, 09:05 AM
Jon Chase Jon Chase is offline
Junior Member
 
Join Date: Aug 2004
Location: Oxford, Ohio, USA
Posts: 18
Default PPH works in applicationContext-xml but not *-servlet.xml

Ok, if PropertyPlaceHolderConfigurer is supposed to work in the *-servlet.xml files, I must be doing something wrong, as I can't seem to get it to work (properties resolve to "${blah}" and not their real values).


Here's my web.xml:
Code:
 <context-param>
  <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/classes/applicationContext.xml
    /WEB-INF/classes/otherContext.xml
   </param-value>
 </context-param>

...

 <servlet>
  <servlet-name>context</servlet-name>
  <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

...

 <servlet>
  <servlet-name>ia</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
 </servlet>
And here's the applicationContext.xml file:
Code:
...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="locations">
  <list>
   <value>classpath:/darwinia.properties</value>
  </list>
 </property>
</bean>
...
And finally, here's my ia-servlet.xml file:
Code:
 <bean id="helloWorldController" class="ia.web.controller.HelloWorldController">
    <property name="displayTranscriptLink"><value>${ia.displayLink}</value></property>
 </bean>
So, instead of ${ia.displayLink} resolving to "true" (or whatever is in the properties file), it resolves to "${ia.displayLink}".

Now, I should mention, the PropertyPlaceHolderConfigurer DOES work for me in other (non *-servlet.xml) context files; i.e. other context files under WEB-INF/classes (like otherContext.xml defined in web.xml above).

I can also see, on application startup in the log output, that the parent context of the ia-servlet context is the root application context (i.e. I can see that the parent context of ia-servlet contains the PropertyPlaceHolderConfigurer definition).

What in the world am I missing?

Jon
Reply With Quote
  #7  
Old Jan 20th, 2005, 09:38 AM
Lawrence Ho Lawrence Ho is offline
Junior Member
 
Join Date: Aug 2004
Location: Hong Kong
Posts: 26
Default

You have shown part of your ia-servlet.xml, which does not contain any definition of PropertyPlaceholderConfigurer. Have you tried to define a separate PropertyPlaceholderConfigurer for your ia-servlet context?

Lawrence
Reply With Quote
  #8  
Old Jan 20th, 2005, 12:47 PM
Jon Chase Jon Chase is offline
Junior Member
 
Join Date: Aug 2004
Location: Oxford, Ohio, USA
Posts: 18
Default

No, I've got the PropertyPlaceHolderConfigurer defined in my root applicationContext.xml file (along with many other beans). I have access to the other beans defined in the root context, so I don't need to declare PropertyPlaceHolderConfigurer in ia-servlet.xml.
Reply With Quote
  #9  
Old Feb 7th, 2005, 07:19 AM
magnusheino magnusheino is offline
Member
 
Join Date: Feb 2005
Posts: 47
Default

Did anyone come up with a solution to this problem?

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/bryggan.properties</value>
</property>
</bean>

I can replace variables defined using this bean declaration in everything but *-servlet.xml files.
Reply With Quote
  #10  
Old Feb 10th, 2005, 03:21 AM
magnusheino magnusheino is offline
Member
 
Join Date: Feb 2005
Posts: 47
Default

Adding the answer from Jurgen on the spring-dev list as referernce for others that might search the forums later on.

Quote:
PropertyPlaceholderConfigurer is an implementation of the
BeanFactoryPostProcessor interface: This interface and its sibling
BeanPostProcessor just apply to the BeanFactory that defines them, that is,
to the application context that defines them.

If you combine multiple config files into a single contextConfigLocation, a
PropertyPlaceholderConfigurer defined in any of the files will apply to all
of the files, because they are loaded into a single application context.

However, a DispatcherServlet has its own application context, just using the
root web application context as parent. Therefore, it needs to define its
own BeanFactoryPostProcessors and/or BeanPostProcessors, in this case its
own PropertyPlaceholderConfigurer.

I believe that this is the better general solution: Each application context
might want to resolve its placeholders against its own properties files,
where applying PropertyPlaceholderConfigurer from parent contexts would
interfere.

Juergen
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reloading servlet.xml handler mappings on the fly? adcspring Web 3 Sep 9th, 2005 06:17 AM
Can any one help me out in integrating spring MVC & Web nlatha Spring Web Flow 3 Aug 12th, 2005 12:36 PM
OpenSessionInViewFilter lazy initialization exception drc Data Access 12 Jun 7th, 2005 03:08 AM


All times are GMT -5. The time now is 02:12 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.