Search in sources :

Example 1 with BeanExpressionContext

use of org.springframework.beans.factory.config.BeanExpressionContext 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 extensionContext) {
    AnnotatedElement element = extensionContext.getElement().get();
    GenericApplicationContext gac = null;
    ApplicationContext applicationContext;
    if (loadContext) {
        applicationContext = SpringExtension.getApplicationContext(extensionContext);
    } else {
        gac = new GenericApplicationContext();
        gac.refresh();
        applicationContext = gac;
    }
    if (!(applicationContext instanceof ConfigurableApplicationContext)) {
        if (logger.isWarnEnabled()) {
            String contextType = (applicationContext != null ? applicationContext.getClass().getName() : "null");
            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();
    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).booleanValue();
    } 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 2 with BeanExpressionContext

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

the class OnExpressionCondition method getMatchOutcome.

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String expression = (String) metadata.getAnnotationAttributes(ConditionalOnExpression.class.getName()).get("value");
    expression = wrapIfNecessary(expression);
    String rawExpression = expression;
    expression = context.getEnvironment().resolvePlaceholders(expression);
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    BeanExpressionResolver resolver = (beanFactory != null) ? beanFactory.getBeanExpressionResolver() : null;
    BeanExpressionContext expressionContext = (beanFactory != null) ? new BeanExpressionContext(beanFactory, null) : null;
    if (resolver == null) {
        resolver = new StandardBeanExpressionResolver();
    }
    boolean result = (Boolean) resolver.evaluate(expression, expressionContext);
    return new ConditionOutcome(result, ConditionMessage.forCondition(ConditionalOnExpression.class, "(" + rawExpression + ")").resultedIn(result));
}
Also used : StandardBeanExpressionResolver(org.springframework.context.expression.StandardBeanExpressionResolver) BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) StandardBeanExpressionResolver(org.springframework.context.expression.StandardBeanExpressionResolver) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext)

Aggregations

BeanExpressionContext (org.springframework.beans.factory.config.BeanExpressionContext)2 BeanExpressionResolver (org.springframework.beans.factory.config.BeanExpressionResolver)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 StandardBeanExpressionResolver (org.springframework.context.expression.StandardBeanExpressionResolver)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1