Consistent support for early placeholder resolution in properties locations
Issue: SPR-10502
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -200,6 +200,7 @@ public class UtilNamespaceHandler extends NamespaceHandlerSupport {
|
|||||||
|
|
||||||
String location = element.getAttribute("location");
|
String location = element.getAttribute("location");
|
||||||
if (StringUtils.hasLength(location)) {
|
if (StringUtils.hasLength(location)) {
|
||||||
|
location = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(location);
|
||||||
String[] locations = StringUtils.commaDelimitedListToStringArray(location);
|
String[] locations = StringUtils.commaDelimitedListToStringArray(location);
|
||||||
builder.addPropertyValue("locations", locations);
|
builder.addPropertyValue("locations", locations);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2011 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -21,6 +21,7 @@ import org.w3c.dom.Element;
|
|||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||||
|
import org.springframework.beans.factory.xml.ParserContext;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,9 +40,10 @@ abstract class AbstractPropertyLoadingBeanDefinitionParser extends AbstractSingl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doParse(Element element, BeanDefinitionBuilder builder) {
|
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||||
String location = element.getAttribute("location");
|
String location = element.getAttribute("location");
|
||||||
if (StringUtils.hasLength(location)) {
|
if (StringUtils.hasLength(location)) {
|
||||||
|
location = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(location);
|
||||||
String[] locations = StringUtils.commaDelimitedListToStringArray(location);
|
String[] locations = StringUtils.commaDelimitedListToStringArray(location);
|
||||||
builder.addPropertyValue("locations", locations);
|
builder.addPropertyValue("locations", locations);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -20,6 +20,7 @@ import org.w3c.dom.Element;
|
|||||||
|
|
||||||
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
|
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||||
|
import org.springframework.beans.factory.xml.ParserContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parser for the <context:property-override/> element.
|
* Parser for the <context:property-override/> element.
|
||||||
@@ -36,8 +37,8 @@ class PropertyOverrideBeanDefinitionParser extends AbstractPropertyLoadingBeanDe
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doParse(Element element, BeanDefinitionBuilder builder) {
|
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||||
super.doParse(element, builder);
|
super.doParse(element, parserContext, builder);
|
||||||
|
|
||||||
builder.addPropertyValue("ignoreInvalidKeys",
|
builder.addPropertyValue("ignoreInvalidKeys",
|
||||||
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));
|
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import org.w3c.dom.Element;
|
|||||||
|
|
||||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||||
|
import org.springframework.beans.factory.xml.ParserContext;
|
||||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@@ -54,8 +55,8 @@ class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBea
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doParse(Element element, BeanDefinitionBuilder builder) {
|
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||||
super.doParse(element, builder);
|
super.doParse(element, parserContext, builder);
|
||||||
|
|
||||||
builder.addPropertyValue("ignoreUnresolvablePlaceholders",
|
builder.addPropertyValue("ignoreUnresolvablePlaceholders",
|
||||||
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));
|
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));
|
||||||
|
|||||||
@@ -16,12 +16,14 @@
|
|||||||
|
|
||||||
package org.springframework.context.config;
|
package org.springframework.context.config;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.beans.FatalBeanException;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
import org.springframework.context.support.GenericXmlApplicationContext;
|
import org.springframework.context.support.GenericXmlApplicationContext;
|
||||||
@@ -89,6 +91,54 @@ public class ContextNamespaceHandlerTests {
|
|||||||
assertEquals("maps", applicationContext.getBean("spam"));
|
assertEquals("maps", applicationContext.getBean("spam"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void propertyPlaceholderLocationWithSystemPropertyForOneLocation() throws Exception {
|
||||||
|
System.setProperty("properties",
|
||||||
|
"classpath*:/org/springframework/context/config/test-*.properties");
|
||||||
|
try {
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||||
|
"contextNamespaceHandlerTests-location-placeholder.xml", getClass());
|
||||||
|
assertEquals("bar", applicationContext.getBean("foo"));
|
||||||
|
assertEquals("foo", applicationContext.getBean("bar"));
|
||||||
|
assertEquals("maps", applicationContext.getBean("spam"));
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
System.clearProperty("properties");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void propertyPlaceholderLocationWithSystemPropertyForMultipleLocations() throws Exception {
|
||||||
|
System.setProperty("properties",
|
||||||
|
"classpath*:/org/springframework/context/config/test-*.properties," +
|
||||||
|
"classpath*:/org/springframework/context/config/empty-*.properties," +
|
||||||
|
"classpath*:/org/springframework/context/config/missing-*.properties");
|
||||||
|
try {
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||||
|
"contextNamespaceHandlerTests-location-placeholder.xml", getClass());
|
||||||
|
assertEquals("bar", applicationContext.getBean("foo"));
|
||||||
|
assertEquals("foo", applicationContext.getBean("bar"));
|
||||||
|
assertEquals("maps", applicationContext.getBean("spam"));
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
System.clearProperty("properties");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void propertyPlaceholderLocationWithSystemPropertyMissing() throws Exception {
|
||||||
|
try {
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||||
|
"contextNamespaceHandlerTests-location-placeholder.xml", getClass());
|
||||||
|
assertEquals("bar", applicationContext.getBean("foo"));
|
||||||
|
assertEquals("foo", applicationContext.getBean("bar"));
|
||||||
|
assertEquals("maps", applicationContext.getBean("spam"));
|
||||||
|
}
|
||||||
|
catch (FatalBeanException ex) {
|
||||||
|
assertTrue(ex.getRootCause() instanceof FileNotFoundException);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void propertyPlaceholderIgnored() throws Exception {
|
public void propertyPlaceholderIgnored() throws Exception {
|
||||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?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"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
|
||||||
|
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
|
||||||
|
|
||||||
|
<context:property-placeholder location="${properties}" file-encoding="ISO-8859-1" trim-values="true"/>
|
||||||
|
|
||||||
|
<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>
|
||||||
Reference in New Issue
Block a user