Tomcat Context-Params ignored in Spring webapp when using PropertyPlaceholder

I previously used the obsolete class org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer to load the properties file from the server file system. I had the following bean defined:

 <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> <property name="locations" value="${config}"/> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="searchContextAttributes" value="true"/> <property name="contextOverride" value="false"/> <property name="ignoreResourceNotFound" value="true"/> <property name="searchSystemEnvironment" value="false"/> </bean> 

config is the argument that is passed when Tomcat starts, i.e.

 -Dconfig=/path/to/application.properties 

For webapp, I also have a context file:

 <Context docBase="/path/to/application.war"> <Parameter name="host" value="localhost" override="false"/> <Parameter name="port" value="8080" override="false"/> </Context> 

If the .properties file specified by the -Dconfig argument contains a property that contains some other bean links, then the value from the .properties file is used, otherwise the value from the XML context file b.

This allowed me to set the default set of properties deployed to WAR, and if necessary, I could specify a .properties file to override certain values.

Now I am updating to use the new property abstractions in Spring 3.1, but I cannot figure out what is the equivalent approach to this?

I have the same context file and war deployed in the same way, and now I have the following in the application:

 <context:property-placeholder location="${config}" system-properties-mode="OVERRIDE" ignore-resource-not-found="true" ignore-unresolvable="true"/> 

This finds and uses the properties from the properties file, BUT does not use the values ​​from the XML context file.

How do I get my application to use context parameters when using this new placeholder?

Thanks.

+7
spring
source share
2 answers

To summarize, the problem is that the context parameters from the servlet context file were not used to eliminate placeholders when using the new Property Placeholder namespace introduced in Spring 3.1.

I figured out a solution with the following

 <context:property-placeholder location="${config}" local-override="true" ignore-resource-not-found="true"/> 

I can specify one or more * .properties files on the local file system using the JVM arg, for example:

 -Dconfig=/path/app.properties 

If the placeholder property cannot be resolved after checking the app.properties file, then the servlet context parameters are checked.

This allows me to have default values ​​using context parameters in the web.xml file and where I need to override these values ​​by specifying the location of the * .properties files using the config JVM argument.

The key to getting this to work was to enable local-override="true" , which is false by default. I'm not quite sure if this makes sense, as the description for this attribute is:

Indicates whether local properties override properties from files. Default is false: properties from files override local defaults.

If the same property key exists in app.properties and web.xml , the value from app.properties .

+4
source share

Spring uses the default property file if no custom property file is specified. If you want to control the .properties file, follow the instructions here .

If you want to take advantage of application.properties, there are two ways to do this.

 <!-- allows for ${} replacement in the spring xml configuration from the system.properties file on the classpath --> <util:properties id="appProperties" location="classpath:application.properties"/> <context:property-placeholder location="classpath:application.properties"/> 
Tag

util allows you to use the property class to read properties in the application. For example:

 @Autowired public MyPropertyReader(Properties appProperties) { String prop1 = appProperties.getProperty("my.address"); String prop2 = appProperties.getProperty("my.version"); } 

If you want to use the values ​​in your context file, use the context: property-placeholder tag. Then you can use your values ​​as

 <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="${jms.primary.server}"/> 

where, for example, jms.primary.server = 172.168.10.18: 6161 in application.properties.

+1
source share

All Articles