1818
1919import java .util .HashSet ;
2020import java .util .Properties ;
21- import java .util .Set ;
2221
2322import org .springframework .beans .BeansException ;
2423import org .springframework .beans .factory .BeanDefinitionStoreException ;
2524import org .springframework .beans .factory .BeanFactory ;
2625import org .springframework .beans .factory .BeanFactoryAware ;
2726import org .springframework .beans .factory .BeanNameAware ;
27+ import org .springframework .beans .factory .InitializingBean ;
2828import org .springframework .core .Constants ;
29- import org .springframework .util .StringUtils ;
3029import org .springframework .util .StringValueResolver ;
30+ import org .springframework .util .PropertyPlaceholderHelper ;
31+ import org .springframework .util .PropertyPlaceholderHelper .PlaceholderResolver ;
3132
3233/**
3334 * A property resource configurer that resolves placeholders in bean property values of
@@ -135,7 +136,6 @@ public class PropertyPlaceholderConfigurer extends PropertyResourceConfigurer
135136
136137 private BeanFactory beanFactory ;
137138
138-
139139 /**
140140 * Set the prefix that a placeholder string starts with.
141141 * The default is "${".
@@ -248,7 +248,6 @@ public void setBeanFactory(BeanFactory beanFactory) {
248248 this .beanFactory = beanFactory ;
249249 }
250250
251-
252251 @ Override
253252 protected void processProperties (ConfigurableListableBeanFactory beanFactoryToProcess , Properties props )
254253 throws BeansException {
@@ -265,7 +264,7 @@ protected void processProperties(ConfigurableListableBeanFactory beanFactoryToPr
265264 try {
266265 visitor .visitBeanDefinition (bd );
267266 }
268- catch (BeanDefinitionStoreException ex ) {
267+ catch (Exception ex ) {
269268 throw new BeanDefinitionStoreException (bd .getResourceDescription (), curName , ex .getMessage ());
270269 }
271270 }
@@ -278,87 +277,6 @@ protected void processProperties(ConfigurableListableBeanFactory beanFactoryToPr
278277 beanFactoryToProcess .addEmbeddedValueResolver (valueResolver );
279278 }
280279
281- /**
282- * Parse the given String value recursively, to be able to resolve
283- * nested placeholders (when resolved property values in turn contain
284- * placeholders again).
285- * @param strVal the String value to parse
286- * @param props the Properties to resolve placeholders against
287- * @param visitedPlaceholders the placeholders that have already been visited
288- * during the current resolution attempt (used to detect circular references
289- * between placeholders). Only non-null if we're parsing a nested placeholder.
290- * @throws BeanDefinitionStoreException if invalid values are encountered
291- * @see #resolvePlaceholder(String, java.util.Properties, int)
292- */
293- protected String parseStringValue (String strVal , Properties props , Set <String > visitedPlaceholders )
294- throws BeanDefinitionStoreException {
295-
296- StringBuilder buf = new StringBuilder (strVal );
297-
298- int startIndex = strVal .indexOf (this .placeholderPrefix );
299- while (startIndex != -1 ) {
300- int endIndex = findPlaceholderEndIndex (buf , startIndex );
301- if (endIndex != -1 ) {
302- String placeholder = buf .substring (startIndex + this .placeholderPrefix .length (), endIndex );
303- if (!visitedPlaceholders .add (placeholder )) {
304- throw new BeanDefinitionStoreException (
305- "Circular placeholder reference '" + placeholder + "' in property definitions" );
306- }
307- // Recursive invocation, parsing placeholders contained in the placeholder key.
308- placeholder = parseStringValue (placeholder , props , visitedPlaceholders );
309- // Now obtain the value for the fully resolved key...
310- String propVal = resolvePlaceholder (placeholder , props , this .systemPropertiesMode );
311- if (propVal != null ) {
312- // Recursive invocation, parsing placeholders contained in the
313- // previously resolved placeholder value.
314- propVal = parseStringValue (propVal , props , visitedPlaceholders );
315- buf .replace (startIndex , endIndex + this .placeholderSuffix .length (), propVal );
316- if (logger .isTraceEnabled ()) {
317- logger .trace ("Resolved placeholder '" + placeholder + "'" );
318- }
319- startIndex = buf .indexOf (this .placeholderPrefix , startIndex + propVal .length ());
320- }
321- else if (this .ignoreUnresolvablePlaceholders ) {
322- // Proceed with unprocessed value.
323- startIndex = buf .indexOf (this .placeholderPrefix , endIndex + this .placeholderSuffix .length ());
324- }
325- else {
326- throw new BeanDefinitionStoreException ("Could not resolve placeholder '" + placeholder + "'" );
327- }
328- visitedPlaceholders .remove (placeholder );
329- }
330- else {
331- startIndex = -1 ;
332- }
333- }
334-
335- return buf .toString ();
336- }
337-
338- private int findPlaceholderEndIndex (CharSequence buf , int startIndex ) {
339- int index = startIndex + this .placeholderPrefix .length ();
340- int withinNestedPlaceholder = 0 ;
341- while (index < buf .length ()) {
342- if (StringUtils .substringMatch (buf , index , this .placeholderSuffix )) {
343- if (withinNestedPlaceholder > 0 ) {
344- withinNestedPlaceholder --;
345- index = index + this .placeholderSuffix .length ();
346- }
347- else {
348- return index ;
349- }
350- }
351- else if (StringUtils .substringMatch (buf , index , this .placeholderPrefix )) {
352- withinNestedPlaceholder ++;
353- index = index + this .placeholderPrefix .length ();
354- }
355- else {
356- index ++;
357- }
358- }
359- return -1 ;
360- }
361-
362280 /**
363281 * Resolve the given placeholder using the given properties, performing
364282 * a system properties check according to the given mode.
@@ -439,16 +357,32 @@ protected String resolveSystemProperty(String key) {
439357 */
440358 private class PlaceholderResolvingStringValueResolver implements StringValueResolver {
441359
442- private final Properties props ;
360+ private final PropertyPlaceholderHelper helper ;
361+
362+ private final PlaceholderResolver resolver ;
443363
444364 public PlaceholderResolvingStringValueResolver (Properties props ) {
445- this .props = props ;
365+ this .helper = new PropertyPlaceholderHelper (placeholderPrefix , placeholderSuffix , ignoreUnresolvablePlaceholders );
366+ this .resolver = new PropertyPlaceholderConfigurerResolver (props );
446367 }
447368
448369 public String resolveStringValue (String strVal ) throws BeansException {
449- String value = parseStringValue (strVal , this .props , new HashSet < String >() );
370+ String value = this . helper . replacePlaceholders (strVal , this .resolver );
450371 return (value .equals (nullValue ) ? null : value );
451372 }
452373 }
453374
375+ private class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver {
376+
377+ private final Properties props ;
378+
379+ private PropertyPlaceholderConfigurerResolver (Properties props ) {
380+ this .props = props ;
381+ }
382+
383+ public String resolvePlaceholder (String placeholderName ) {
384+ return PropertyPlaceholderConfigurer .this .resolvePlaceholder (placeholderName , props , systemPropertiesMode );
385+ }
386+ }
387+
454388}
0 commit comments