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