📄 propertyplaceholderconfigurer.java
字号:
protected void parseBeanDefinition(Properties props, BeanDefinition beanDefinition) {
MutablePropertyValues pvs = beanDefinition.getPropertyValues();
if (pvs != null) {
parsePropertyValues(props, pvs);
}
ConstructorArgumentValues cas = beanDefinition.getConstructorArgumentValues();
if (cas != null) {
parseIndexedArgumentValues(props, cas.getIndexedArgumentValues());
parseGenericArgumentValues(props, cas.getGenericArgumentValues());
}
}
protected void parsePropertyValues(Properties props, MutablePropertyValues pvs) {
PropertyValue[] pvArray = pvs.getPropertyValues();
for (int i = 0; i < pvArray.length; i++) {
PropertyValue pv = pvArray[i];
Object newVal = parseValue(props, pv.getValue());
if (!ObjectUtils.nullSafeEquals(newVal, pv.getValue())) {
pvs.addPropertyValue(pv.getName(), newVal);
}
}
}
protected void parseIndexedArgumentValues(Properties props, Map ias) {
for (Iterator it = ias.values().iterator(); it.hasNext();) {
ConstructorArgumentValues.ValueHolder valueHolder =
(ConstructorArgumentValues.ValueHolder) it.next();
Object newVal = parseValue(props, valueHolder.getValue());
if (!ObjectUtils.nullSafeEquals(newVal, valueHolder.getValue())) {
valueHolder.setValue(newVal);
}
}
}
protected void parseGenericArgumentValues(Properties props, Set gas) {
for (Iterator it = gas.iterator(); it.hasNext();) {
ConstructorArgumentValues.ValueHolder valueHolder =
(ConstructorArgumentValues.ValueHolder) it.next();
Object newVal = parseValue(props, valueHolder.getValue());
if (!ObjectUtils.nullSafeEquals(newVal, valueHolder.getValue())) {
valueHolder.setValue(newVal);
}
}
}
protected Object parseValue(Properties props, Object value) {
if (value instanceof String) {
return parseString(props, (String) value);
}
else if (value instanceof RuntimeBeanReference) {
RuntimeBeanReference ref = (RuntimeBeanReference) value;
String newBeanName = parseString(props, ref.getBeanName());
if (!newBeanName.equals(ref.getBeanName())) {
return new RuntimeBeanReference(newBeanName);
}
}
else if (value instanceof List) {
parseList(props, (List) value);
}
else if (value instanceof Set) {
parseSet(props, (Set) value);
}
else if (value instanceof Map) {
parseMap(props, (Map) value);
}
else if (value instanceof BeanDefinition) {
parseBeanDefinition(props, (BeanDefinition) value);
}
else if (value instanceof BeanDefinitionHolder) {
parseBeanDefinition(props, ((BeanDefinitionHolder) value).getBeanDefinition());
}
return value;
}
/**
* Parse the given List, resolving its values if necessary.
*/
protected void parseList(Properties props, List listVal) {
for (int i = 0; i < listVal.size(); i++) {
Object elem = listVal.get(i);
Object newVal = parseValue(props, elem);
if (!ObjectUtils.nullSafeEquals(newVal, elem)) {
listVal.set(i, newVal);
}
}
}
/**
* Parse the given Set, resolving its values if necessary.
*/
protected void parseSet(Properties props, Set setVal) {
for (Iterator it = new HashSet(setVal).iterator(); it.hasNext();) {
Object elem = it.next();
Object newVal = parseValue(props, elem);
if (!ObjectUtils.nullSafeEquals(newVal, elem)) {
setVal.remove(elem);
setVal.add(newVal);
}
}
}
/**
* Parse the given Map, resolving its values if necessary.
*/
protected void parseMap(Properties props, Map mapVal) {
for (Iterator it = new HashMap(mapVal).entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object newKey = parseValue(props, key);
boolean isNewKey = !ObjectUtils.nullSafeEquals(key, newKey);
Object val = entry.getValue();
Object newVal = parseValue(props, val);
if (isNewKey) {
mapVal.remove(key);
}
if (isNewKey || !ObjectUtils.nullSafeEquals(newVal, val)) {
mapVal.put(newKey, newVal);
}
}
}
/**
* Parse the given String, resolving its values if necessary.
* @see #parseString(java.util.Properties, String, String)
*/
protected String parseString(Properties props, String strVal)
throws BeansException {
return parseString(props, strVal, null);
}
/**
* Parse values recursively to be able to resolve cross-references between placeholder values.
*/
protected String parseString(Properties props, String strVal, String originalPlaceholder)
throws BeansException {
StringBuffer buf = new StringBuffer(strVal);
// The following code does not use JDK 1.4's StringBuffer.indexOf(String)
// method to retain JDK 1.3 compatibility. The slight loss in performance
// is not really relevant, as this code will typically just run on startup.
int startIndex = strVal.indexOf(this.placeholderPrefix);
while (startIndex != -1) {
int endIndex = buf.toString().indexOf(
this.placeholderSuffix, startIndex + this.placeholderPrefix.length());
if (endIndex != -1) {
String placeholder = buf.substring(startIndex + this.placeholderPrefix.length(), endIndex);
String originalPlaceholderToUse = null;
if (originalPlaceholder != null) {
originalPlaceholderToUse = originalPlaceholder;
if (placeholder.equals(originalPlaceholder)) {
throw new BeanDefinitionStoreException(
"Circular placeholder reference '" + placeholder + "' in property definitions [" + props + "]");
}
}
else {
originalPlaceholderToUse = placeholder;
}
String propVal = resolvePlaceholder(placeholder, props, this.systemPropertiesMode);
if (propVal != null) {
propVal = parseString(props, propVal, originalPlaceholderToUse);
if (logger.isDebugEnabled()) {
logger.debug("Resolving placeholder '" + placeholder + "' to [" + propVal + "]");
}
buf.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
startIndex = buf.toString().indexOf(this.placeholderPrefix, startIndex + propVal.length());
}
else if (this.ignoreUnresolvablePlaceholders) {
// proceed with unprocessed value
startIndex = buf.toString().indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
}
else {
throw new BeanDefinitionStoreException("Could not resolve placeholder '" + placeholder + "'");
}
}
else {
startIndex = -1;
}
}
return buf.toString();
}
/**
* Resolve the given placeholder using the given properties, performing
* a system properties check according to the given mode.
* <p>Default implementation delegates to <code>resolvePlaceholder
* (placeholder, props)</code> before/after the system properties check.
* <p>Subclasses can override this for custom resolution strategies,
* including customized points for the system properties check.
* @param placeholder the placeholder to resolve
* @param props the merged properties of this configurer
* @param systemPropertiesMode the system properties mode,
* according to the constants in this class
* @return the resolved value, of null if none
* @see #setSystemPropertiesMode
* @see System#getProperty
* @see #resolvePlaceholder(String, java.util.Properties)
*/
protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) {
String propVal = null;
if (systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) {
propVal = System.getProperty(placeholder);
}
if (propVal == null) {
propVal = resolvePlaceholder(placeholder, props);
}
if (propVal == null && systemPropertiesMode == SYSTEM_PROPERTIES_MODE_FALLBACK) {
propVal = System.getProperty(placeholder);
}
return propVal;
}
/**
* Resolve the given placeholder using the given properties.
* Default implementation simply checks for a corresponding property key.
* <p>Subclasses can override this for customized placeholder-to-key mappings
* or custom resolution strategies, possibly just using the given properties
* as fallback.
* <p>Note that system properties will still be checked before respectively
* after this method is invoked, according to the system properties mode.
* @param placeholder the placeholder to resolve
* @param props the merged properties of this configurer
* @return the resolved value, of null if none
* @see #setSystemPropertiesMode
*/
protected String resolvePlaceholder(String placeholder, Properties props) {
return props.getProperty(placeholder);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -