RESOLVED - issue SPR-4661: Improve context-property-placeholder configurability
Added new features to property override and placeholders (order, locations, system-properties-mode, ignore-*)
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.context.config;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -31,30 +32,76 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Dave Syer
|
||||
* @since 2.5.6
|
||||
*/
|
||||
public class ContextNamespaceHandlerTests {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Before
|
||||
public void createAppContext() {
|
||||
applicationContext = new ClassPathXmlApplicationContext("contextNamespaceHandlerTests.xml", getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyPlaceholder() throws Exception {
|
||||
Map beans = applicationContext.getBeansOfType(PropertyPlaceholderConfigurer.class);
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-replace.xml", getClass());
|
||||
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
|
||||
.getBeansOfType(PropertyPlaceholderConfigurer.class);
|
||||
assertFalse("No PropertyPlaceHolderConfigurer found", beans.isEmpty());
|
||||
String s = (String) applicationContext.getBean("string");
|
||||
assertEquals("No properties replaced", "bar", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyPlaceholderSystemProperties() throws Exception {
|
||||
String value = System.setProperty("foo", "spam");
|
||||
try {
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-system.xml", getClass());
|
||||
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
|
||||
.getBeansOfType(PropertyPlaceholderConfigurer.class);
|
||||
assertFalse("No PropertyPlaceHolderConfigurer found", beans.isEmpty());
|
||||
String s = (String) applicationContext.getBean("string");
|
||||
assertEquals("No properties replaced", "spam", s);
|
||||
} finally {
|
||||
if (value!=null) {
|
||||
System.setProperty("foo", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyPlaceholderLocation() throws Exception {
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-location.xml", getClass());
|
||||
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
|
||||
.getBeansOfType(PropertyPlaceholderConfigurer.class);
|
||||
assertFalse("No PropertyPlaceHolderConfigurer found", beans.isEmpty());
|
||||
String s = (String) applicationContext.getBean("foo");
|
||||
assertEquals("No properties replaced", "bar", s);
|
||||
s = (String) applicationContext.getBean("bar");
|
||||
assertEquals("No properties replaced", "foo", s);
|
||||
s = (String) applicationContext.getBean("spam");
|
||||
assertEquals("No properties replaced", "maps", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyPlaceholderIgnored() throws Exception {
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-replace-ignore.xml", getClass());
|
||||
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
|
||||
.getBeansOfType(PropertyPlaceholderConfigurer.class);
|
||||
assertFalse("No PropertyPlaceHolderConfigurer found", beans.isEmpty());
|
||||
String s = (String) applicationContext.getBean("string");
|
||||
assertEquals("Properties replaced", "${bar}", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyOverride() throws Exception {
|
||||
Map beans = applicationContext.getBeansOfType(PropertyOverrideConfigurer.class);
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"contextNamespaceHandlerTests-override.xml", getClass());
|
||||
Map<String, PropertyOverrideConfigurer> beans = applicationContext
|
||||
.getBeansOfType(PropertyOverrideConfigurer.class);
|
||||
assertFalse("No PropertyOverrideConfigurer found", beans.isEmpty());
|
||||
Date date = (Date) applicationContext.getBean("date");
|
||||
assertEquals("No properties overriden", 42, date.getMinutes());
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
assertEquals("No properties overriden", 42, calendar.get(Calendar.MINUTE));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
|
||||
|
||||
<context:property-placeholder
|
||||
location="classpath:/org/springframework/context/config/test-*.properties, classpath:/org/springframework/context/config/empty-*.properties, classpath:/org/springframework/context/config/missing-*.properties" />
|
||||
|
||||
<bean id="foo" class="java.lang.String">
|
||||
<constructor-arg value="${foo}" />
|
||||
</bean>
|
||||
|
||||
<bean id="bar" class="java.lang.String">
|
||||
<constructor-arg value="${bar}" />
|
||||
</bean>
|
||||
|
||||
<bean id="spam" class="java.lang.String">
|
||||
<constructor-arg value="${spam}" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
|
||||
|
||||
<util:properties id="overrideProps">
|
||||
<prop key="date.minutes">42</prop>
|
||||
</util:properties>
|
||||
|
||||
<context:property-override properties-ref="overrideProps" order="1" />
|
||||
|
||||
<bean id="date" class="java.util.Date">
|
||||
<property name="minutes" value="10"/>
|
||||
</bean>
|
||||
|
||||
<context:property-override location="not/here" ignore-resource-not-found="true" order="2"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
|
||||
|
||||
<util:properties id="placeholderProps">
|
||||
<prop key="foo">bar</prop>
|
||||
</util:properties>
|
||||
|
||||
<context:property-placeholder properties-ref="placeholderProps" ignore-unresolvable="true"/>
|
||||
|
||||
<bean id="string" class="java.lang.String">
|
||||
<constructor-arg value="${bar}"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -2,26 +2,23 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
|
||||
|
||||
<util:properties id="placeholderProps">
|
||||
<prop key="foo">bar</prop>
|
||||
</util:properties>
|
||||
|
||||
<context:property-placeholder properties-ref="placeholderProps"/>
|
||||
<util:properties id="emptyProps"/>
|
||||
|
||||
<context:property-placeholder properties-ref="placeholderProps" order="2"/>
|
||||
|
||||
<context:property-placeholder properties-ref="emptyProps" order="1" ignore-unresolvable="true"/>
|
||||
|
||||
<context:property-placeholder location="not/here" ignore-resource-not-found="true" order="2"/>
|
||||
|
||||
<bean id="string" class="java.lang.String">
|
||||
<constructor-arg value="${foo}"/>
|
||||
</bean>
|
||||
|
||||
<util:properties id="overrideProps">
|
||||
<prop key="date.minutes">42</prop>
|
||||
</util:properties>
|
||||
|
||||
<context:property-override properties-ref="overrideProps"/>
|
||||
|
||||
<bean id="date" class="java.util.Date">
|
||||
<property name="minutes" value="10"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
|
||||
|
||||
<util:properties id="placeholderProps">
|
||||
<prop key="foo">bar</prop>
|
||||
</util:properties>
|
||||
|
||||
<context:property-placeholder properties-ref="placeholderProps" system-properties-mode="OVERRIDE"/>
|
||||
|
||||
<bean id="string" class="java.lang.String">
|
||||
<constructor-arg value="${foo}"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
bar=foo
|
||||
spam=maps
|
||||
@@ -0,0 +1 @@
|
||||
foo=bar
|
||||
Reference in New Issue
Block a user