Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add properties to property placeholder

I have an application where a property placeholder is used to read properties, configured in applicationContext.xml:

...
<context:property-placeholder
     location="classpath*:META-INF/spring/*.properties"/> 
...

The application runs in an Tomcat and uses the parameter defined in context.xml. The application access this parameter like normal properties (@Value(${cfma.applicationUrl})). This works

In my test cases I do not have this tomcat properties, so I want to add them "manually" to the application context. But also load the normal applicationContext.xml

testContext.xml:

<import resource="classpath:/META-INF/spring/applicationContext.xml" />
<context:property-placeholder properties-ref="simulatedTomcatProperties"/>
<util:properties id="simulatedTomcatProperties">
   <prop key="cfmt.applicationBaseUrl">localhost:8080/cfmt</prop>
</util:properties>

Now I have two context:property-placeholder and this does not work (of course) – So my question is, who can I extend the properties in the “normal” property-placeholder in my test?


More Explanation of what I need:

  • The productive environment (as well as the development environment) defines some properties via Tomcat parameter. Therefore they are not included in any properties file, but nerveless they can be accessed like normal properties (@Value(${cfma.applicationUrl})). Moreover there must not be any Fallback, if the properties are not defined in the Tomcat, the application must not start!
  • In the test cases (that use the spring context) I must some how insert the property (cfma.applicationUrl) so that it can be injected in the annotated variables. But if I add an second context:property-placeholder they are not merged:

@See Comments on https://jira.springsource.org/browse/SPR-4881 -- they explain that behaviour.


When I talk about Tomcat parameter I am talking about somethink like this:

context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Parameter name="cfmt.applicationBaseUrl"
          value="http://localhost/demoApp" override="false"/>
</Context>
like image 963
Ralph Avatar asked Jun 17 '11 14:06

Ralph


People also ask

How to use property placeholder in spring?

The context:property-placeholder tag is used to externalize properties in a separate file. It automatically configures PropertyPlaceholderConfigurer , which replaces the ${} placeholders, which are resolved against a specified properties file (as a Spring resource location).

What is Property placeholder in mule 4?

Secure Property Placeholder is an important standard for keeping our sensitive data like User ID and Password secure (encrypted/cypher-text) in the Property file. Data is stored in the property file as key value pair. This property file can store information like User ID, Password, Tokens, Keys etc.

What can I use instead of PropertyPlaceholderConfigurer?

Just use: final var configurer = new PropertySourcesPlaceholderConfigurer(); configurer. setProperties(properties);

What is PropertyPlaceholderConfigurer in spring?

A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions. The default placeholder syntax follows the Ant / Log4J / JSP EL style: ${...}


1 Answers

Not sure if this will help, but what I do in a similar situation is have 2 app.properties files with the same name, one in sec/test/resources and the other in src/main/resources. Now during testing the first is loaded because the test classes are first on the classpath, but when I deploy only the main one is there and so it is loaded.

like image 150
Duncan McGregor Avatar answered Nov 03 '22 08:11

Duncan McGregor