|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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>
Code:
... <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/darwinia.properties</value> </list> </property> </bean> ... Code:
<bean id="helloWorldController" class="ia.web.controller.HelloWorldController">
<property name="displayTranscriptLink"><value>${ia.displayLink}</value></property>
</bean>
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 |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
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.
|
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
Adding the answer from Jurgen on the spring-dev list as referernce for others that might search the forums later on.
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |