Search in sources :

Example 6 with BeanExpressionResolver

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

the class AbstractExpressionEvaluatingCondition method evaluateExpression.

private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext, Class<A> annotationType, ExtensionContext context) {
    Assert.state(context.getElement().isPresent(), "No AnnotatedElement");
    AnnotatedElement element = context.getElement().get();
    GenericApplicationContext gac = null;
    ApplicationContext applicationContext;
    if (loadContext) {
        applicationContext = SpringExtension.getApplicationContext(context);
    } else {
        gac = new GenericApplicationContext();
        gac.refresh();
        applicationContext = gac;
    }
    if (!(applicationContext instanceof ConfigurableApplicationContext)) {
        if (logger.isWarnEnabled()) {
            String contextType = applicationContext.getClass().getName();
            logger.warn(String.format("@%s(\"%s\") could not be evaluated on [%s] since the test " + "ApplicationContext [%s] is not a ConfigurableApplicationContext", annotationType.getSimpleName(), expression, element, contextType));
        }
        return false;
    }
    ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver();
    Assert.state(expressionResolver != null, "No BeanExpressionResolver");
    BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null);
    Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression), beanExpressionContext);
    if (gac != null) {
        gac.close();
    }
    if (result instanceof Boolean) {
        return (Boolean) result;
    } else if (result instanceof String) {
        String str = ((String) result).trim().toLowerCase();
        if ("true".equals(str)) {
            return true;
        }
        Assert.state("false".equals(str), () -> String.format("@%s(\"%s\") on %s must evaluate to \"true\" or \"false\", not \"%s\"", annotationType.getSimpleName(), expression, element, result));
        return false;
    } else {
        String message = String.format("@%s(\"%s\") on %s must evaluate to a String or a Boolean, not %s", annotationType.getSimpleName(), expression, element, (result != null ? result.getClass().getName() : "null"));
        throw new IllegalStateException(message);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AnnotatedElement(java.lang.reflect.AnnotatedElement) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext)

Example 7 with BeanExpressionResolver

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

the class AbstractNamedValueMethodArgumentResolver method resolveStringValue.

/**
	 * Resolve the given annotation-specified value,
	 * potentially containing placeholders and expressions.
	 */
private Object resolveStringValue(String value) {
    if (this.configurableBeanFactory == 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)

Example 8 with BeanExpressionResolver

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

the class AbstractNamedValueArgumentResolver method resolveStringValue.

/**
	 * Resolve the given annotation-specified value,
	 * potentially containing placeholders and expressions.
	 */
private Object resolveStringValue(String value) {
    if (this.configurableBeanFactory == 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)

Example 9 with BeanExpressionResolver

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

the class AbstractNamedValueMethodArgumentResolver method resolveStringValue.

/**
	 * Resolve the given annotation-specified value,
	 * potentially containing placeholders and expressions.
	 */
private Object resolveStringValue(String value) {
    if (this.configurableBeanFactory == 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)

Example 10 with BeanExpressionResolver

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

the class MessagingMethodInvokerHelper method setBeanFactory.

@Override
public void setBeanFactory(BeanFactory beanFactory) {
    super.setBeanFactory(beanFactory);
    this.messageHandlerMethodFactory.setBeanFactory(beanFactory);
    if (beanFactory instanceof ConfigurableListableBeanFactory) {
        BeanExpressionResolver beanExpressionResolver = ((ConfigurableListableBeanFactory) beanFactory).getBeanExpressionResolver();
        if (beanExpressionResolver != null) {
            this.resolver = beanExpressionResolver;
        }
        this.expressionContext = new BeanExpressionContext((ConfigurableListableBeanFactory) beanFactory, null);
    }
}
Also used : StandardBeanExpressionResolver(org.springframework.context.expression.StandardBeanExpressionResolver) BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext)

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