001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.factory.config;
018
019import org.springframework.beans.factory.BeanDefinitionStoreException;
020import org.springframework.beans.factory.BeanFactory;
021import org.springframework.beans.factory.BeanFactoryAware;
022import org.springframework.beans.factory.BeanNameAware;
023import org.springframework.lang.Nullable;
024import org.springframework.util.StringValueResolver;
025
026/**
027 * Abstract base class for property resource configurers that resolve placeholders
028 * in bean definition property values. Implementations <em>pull</em> values from a
029 * properties file or other {@linkplain org.springframework.core.env.PropertySource
030 * property source} into bean definitions.
031 *
032 * <p>The default placeholder syntax follows the Ant / Log4J / JSP EL style:
033 *
034 * <pre class="code">${...}</pre>
035 *
036 * Example XML bean definition:
037 *
038 * <pre class="code">
039 * &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"/&gt;
040 *   &lt;property name="driverClassName" value="${driver}"/&gt;
041 *   &lt;property name="url" value="jdbc:${dbname}"/&gt;
042 * &lt;/bean&gt;
043 * </pre>
044 *
045 * Example properties file:
046 *
047 * <pre class="code">driver=com.mysql.jdbc.Driver
048 * dbname=mysql:mydb</pre>
049 *
050 * Annotated bean definitions may take advantage of property replacement using
051 * the {@link org.springframework.beans.factory.annotation.Value @Value} annotation:
052 *
053 * <pre class="code">@Value("${person.age}")</pre>
054 *
055 * Implementations check simple property values, lists, maps, props, and bean names
056 * in bean references. Furthermore, placeholder values can also cross-reference
057 * other placeholders, like:
058 *
059 * <pre class="code">rootPath=myrootdir
060 * subPath=${rootPath}/subdir</pre>
061 *
062 * In contrast to {@link PropertyOverrideConfigurer}, subclasses of this type allow
063 * filling in of explicit placeholders in bean definitions.
064 *
065 * <p>If a configurer cannot resolve a placeholder, a {@link BeanDefinitionStoreException}
066 * will be thrown. If you want to check against multiple properties files, specify multiple
067 * resources via the {@link #setLocations locations} property. You can also define multiple
068 * configurers, each with its <em>own</em> placeholder syntax. Use {@link
069 * #ignoreUnresolvablePlaceholders} to intentionally suppress throwing an exception if a
070 * placeholder cannot be resolved.
071 *
072 * <p>Default property values can be defined globally for each configurer instance
073 * via the {@link #setProperties properties} property, or on a property-by-property basis
074 * using the default value separator which is {@code ":"} by default and
075 * customizable via {@link #setValueSeparator(String)}.
076 *
077 * <p>Example XML property with default value:
078 *
079 * <pre class="code">
080 *   <property name="url" value="jdbc:${dbname:defaultdb}"/>
081 * </pre>
082 *
083 * @author Chris Beams
084 * @author Juergen Hoeller
085 * @since 3.1
086 * @see PropertyPlaceholderConfigurer
087 * @see org.springframework.context.support.PropertySourcesPlaceholderConfigurer
088 */
089public abstract class PlaceholderConfigurerSupport extends PropertyResourceConfigurer
090                implements BeanNameAware, BeanFactoryAware {
091
092        /** Default placeholder prefix: {@value}. */
093        public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
094
095        /** Default placeholder suffix: {@value}. */
096        public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
097
098        /** Default value separator: {@value}. */
099        public static final String DEFAULT_VALUE_SEPARATOR = ":";
100
101
102        /** Defaults to {@value #DEFAULT_PLACEHOLDER_PREFIX}. */
103        protected String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
104
105        /** Defaults to {@value #DEFAULT_PLACEHOLDER_SUFFIX}. */
106        protected String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;
107
108        /** Defaults to {@value #DEFAULT_VALUE_SEPARATOR}. */
109        @Nullable
110        protected String valueSeparator = DEFAULT_VALUE_SEPARATOR;
111
112        protected boolean trimValues = false;
113
114        @Nullable
115        protected String nullValue;
116
117        protected boolean ignoreUnresolvablePlaceholders = false;
118
119        @Nullable
120        private String beanName;
121
122        @Nullable
123        private BeanFactory beanFactory;
124
125
126        /**
127         * Set the prefix that a placeholder string starts with.
128         * The default is {@value #DEFAULT_PLACEHOLDER_PREFIX}.
129         */
130        public void setPlaceholderPrefix(String placeholderPrefix) {
131                this.placeholderPrefix = placeholderPrefix;
132        }
133
134        /**
135         * Set the suffix that a placeholder string ends with.
136         * The default is {@value #DEFAULT_PLACEHOLDER_SUFFIX}.
137         */
138        public void setPlaceholderSuffix(String placeholderSuffix) {
139                this.placeholderSuffix = placeholderSuffix;
140        }
141
142        /**
143         * Specify the separating character between the placeholder variable
144         * and the associated default value, or {@code null} if no such
145         * special character should be processed as a value separator.
146         * The default is {@value #DEFAULT_VALUE_SEPARATOR}.
147         */
148        public void setValueSeparator(@Nullable String valueSeparator) {
149                this.valueSeparator = valueSeparator;
150        }
151
152        /**
153         * Specify whether to trim resolved values before applying them,
154         * removing superfluous whitespace from the beginning and end.
155         * <p>Default is {@code false}.
156         * @since 4.3
157         */
158        public void setTrimValues(boolean trimValues) {
159                this.trimValues = trimValues;
160        }
161
162        /**
163         * Set a value that should be treated as {@code null} when resolved
164         * as a placeholder value: e.g. "" (empty String) or "null".
165         * <p>Note that this will only apply to full property values,
166         * not to parts of concatenated values.
167         * <p>By default, no such null value is defined. This means that
168         * there is no way to express {@code null} as a property value
169         * unless you explicitly map a corresponding value here.
170         */
171        public void setNullValue(String nullValue) {
172                this.nullValue = nullValue;
173        }
174
175        /**
176         * Set whether to ignore unresolvable placeholders.
177         * <p>Default is "false": An exception will be thrown if a placeholder fails
178         * to resolve. Switch this flag to "true" in order to preserve the placeholder
179         * String as-is in such a case, leaving it up to other placeholder configurers
180         * to resolve it.
181         */
182        public void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) {
183                this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
184        }
185
186        /**
187         * Only necessary to check that we're not parsing our own bean definition,
188         * to avoid failing on unresolvable placeholders in properties file locations.
189         * The latter case can happen with placeholders for system properties in
190         * resource locations.
191         * @see #setLocations
192         * @see org.springframework.core.io.ResourceEditor
193         */
194        @Override
195        public void setBeanName(String beanName) {
196                this.beanName = beanName;
197        }
198
199        /**
200         * Only necessary to check that we're not parsing our own bean definition,
201         * to avoid failing on unresolvable placeholders in properties file locations.
202         * The latter case can happen with placeholders for system properties in
203         * resource locations.
204         * @see #setLocations
205         * @see org.springframework.core.io.ResourceEditor
206         */
207        @Override
208        public void setBeanFactory(BeanFactory beanFactory) {
209                this.beanFactory = beanFactory;
210        }
211
212
213        protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
214                        StringValueResolver valueResolver) {
215
216                BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
217
218                String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
219                for (String curName : beanNames) {
220                        // Check that we're not parsing our own bean definition,
221                        // to avoid failing on unresolvable placeholders in properties file locations.
222                        if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
223                                BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
224                                try {
225                                        visitor.visitBeanDefinition(bd);
226                                }
227                                catch (Exception ex) {
228                                        throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage(), ex);
229                                }
230                        }
231                }
232
233                // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
234                beanFactoryToProcess.resolveAliases(valueResolver);
235
236                // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
237                beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
238        }
239
240}