Search in sources :

Example 1 with BeanExpressionResolver

use of org.springframework.beans.factory.config.BeanExpressionResolver in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testExpressionInStringArray.

@Test
public void testExpressionInStringArray() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class);
    when(beanExpressionResolver.evaluate(eq("#{foo}"), ArgumentMatchers.any(BeanExpressionContext.class))).thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties");
    bf.setBeanExpressionResolver(beanExpressionResolver);
    RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("locations", new String[] { "#{foo}" });
    rbd.setPropertyValues(pvs);
    bf.registerBeanDefinition("myProperties", rbd);
    Properties properties = (Properties) bf.getBean("myProperties");
    assertEquals("bar", properties.getProperty("foo"));
}
Also used : BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Properties(java.util.Properties) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext) Test(org.junit.Test)

Example 2 with BeanExpressionResolver

use of org.springframework.beans.factory.config.BeanExpressionResolver in project spring-cloud-stream by spring-cloud.

the class ApplicationMetricsProperties method buildExportProperties.

private Map<String, Object> buildExportProperties() {
    Map<String, Object> props = new HashMap<>();
    if (!ObjectUtils.isEmpty(this.properties)) {
        Map<String, String> target = bindProperties();
        BeanExpressionResolver beanExpressionResolver = ((ConfigurableApplicationContext) applicationContext).getBeanFactory().getBeanExpressionResolver();
        BeanExpressionContext expressionContext = new BeanExpressionContext(((ConfigurableApplicationContext) applicationContext).getBeanFactory(), null);
        for (Entry<String, String> entry : target.entrySet()) {
            if (isMatch(entry.getKey(), this.properties, null)) {
                String stringValue = ObjectUtils.nullSafeToString(entry.getValue());
                Object exportedValue = null;
                if (stringValue != null) {
                    exportedValue = stringValue.startsWith("#{") ? beanExpressionResolver.evaluate(environment.resolvePlaceholders(stringValue), expressionContext) : environment.resolvePlaceholders(stringValue);
                }
                props.put(entry.getKey(), exportedValue);
            }
        }
    }
    return props;
}
Also used : BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) HashMap(java.util.HashMap) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext)

Example 3 with BeanExpressionResolver

use of org.springframework.beans.factory.config.BeanExpressionResolver in project spring-framework by spring-projects.

the class AbstractNamedValueMethodArgumentResolver method resolveEmbeddedValuesAndExpressions.

/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
@Nullable
private Object resolveEmbeddedValuesAndExpressions(String value) {
    if (this.configurableBeanFactory == null || this.expressionContext == null) {
        return value;
    }
    String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
    BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
    if (exprResolver == null) {
        return value;
    }
    return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
Also used : BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) Nullable(org.springframework.lang.Nullable)

Example 4 with BeanExpressionResolver

use of org.springframework.beans.factory.config.BeanExpressionResolver in project spring-boot by spring-projects.

the class OnExpressionCondition method evaluateExpression.

private Boolean evaluateExpression(ConfigurableListableBeanFactory beanFactory, String expression) {
    BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver();
    if (resolver == null) {
        resolver = new StandardBeanExpressionResolver();
    }
    BeanExpressionContext expressionContext = new BeanExpressionContext(beanFactory, null);
    Object result = resolver.evaluate(expression, expressionContext);
    return (result != null && (boolean) result);
}
Also used : StandardBeanExpressionResolver(org.springframework.context.expression.StandardBeanExpressionResolver) BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) StandardBeanExpressionResolver(org.springframework.context.expression.StandardBeanExpressionResolver) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext)

Example 5 with BeanExpressionResolver

use of org.springframework.beans.factory.config.BeanExpressionResolver in project spring-framework by spring-projects.

the class AbstractNamedValueMethodArgumentResolver method resolveEmbeddedValuesAndExpressions.

/**
 * Resolve the given annotation-specified value,
 * potentially containing placeholders and expressions.
 */
@Nullable
private Object resolveEmbeddedValuesAndExpressions(String value) {
    if (this.configurableBeanFactory == null || this.expressionContext == null) {
        return value;
    }
    String placeholdersResolved = this.configurableBeanFactory.resolveEmbeddedValue(value);
    BeanExpressionResolver exprResolver = this.configurableBeanFactory.getBeanExpressionResolver();
    if (exprResolver == null) {
        return value;
    }
    return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
}
Also used : BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) Nullable(org.springframework.lang.Nullable)

Aggregations

BeanExpressionResolver (org.springframework.beans.factory.config.BeanExpressionResolver)13 BeanExpressionContext (org.springframework.beans.factory.config.BeanExpressionContext)6 Nullable (org.springframework.lang.Nullable)4 Properties (java.util.Properties)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 StandardBeanExpressionResolver (org.springframework.context.expression.StandardBeanExpressionResolver)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1