001/*
002 * Copyright 2002-2016 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.util.StringValueResolver;
020
021/**
022 * {@link StringValueResolver} adapter for resolving placeholders and
023 * expressions against a {@link ConfigurableBeanFactory}.
024 *
025 * <p>Note that this adapter resolves expressions as well, in contrast
026 * to the {@link ConfigurableBeanFactory#resolveEmbeddedValue} method.
027 * The {@link BeanExpressionContext} used is for the plain bean factory,
028 * with no scope specified for any contextual objects to access.
029 *
030 * @author Juergen Hoeller
031 * @since 4.3
032 * @see ConfigurableBeanFactory#resolveEmbeddedValue(String)
033 * @see ConfigurableBeanFactory#getBeanExpressionResolver()
034 * @see BeanExpressionContext
035 */
036public class EmbeddedValueResolver implements StringValueResolver {
037
038        private final BeanExpressionContext exprContext;
039
040        private final BeanExpressionResolver exprResolver;
041
042
043        public EmbeddedValueResolver(ConfigurableBeanFactory beanFactory) {
044                this.exprContext = new BeanExpressionContext(beanFactory, null);
045                this.exprResolver = beanFactory.getBeanExpressionResolver();
046        }
047
048
049        @Override
050        public String resolveStringValue(String strVal) {
051                String value = this.exprContext.getBeanFactory().resolveEmbeddedValue(strVal);
052                if (this.exprResolver != null && value != null) {
053                        Object evaluated = this.exprResolver.evaluate(value, this.exprContext);
054                        value = (evaluated != null ? evaluated.toString() : null);
055                }
056                return value;
057        }
058
059}