Search in sources :

Example 1 with StandardBeanExpressionResolver

use of org.springframework.context.expression.StandardBeanExpressionResolver in project spring-framework by spring-projects.

the class AbstractApplicationContext method prepareBeanFactory.

/**
	 * Configure the factory's standard context characteristics,
	 * such as the context's ClassLoader and post-processors.
	 * @param beanFactory the BeanFactory to configure
	 */
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    // Tell the internal bean factory to use the context's class loader etc.
    beanFactory.setBeanClassLoader(getClassLoader());
    beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
    beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
    // Configure the bean factory with context callbacks.
    beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
    beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
    beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
    beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
    // BeanFactory interface not registered as resolvable type in a plain factory.
    // MessageSource registered (and found for autowiring) as a bean.
    beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
    beanFactory.registerResolvableDependency(ResourceLoader.class, this);
    beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
    beanFactory.registerResolvableDependency(ApplicationContext.class, this);
    // Register early post-processor for detecting inner beans as ApplicationListeners.
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
    // Detect a LoadTimeWeaver and prepare for weaving, if found.
    if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
        beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
        // Set a temporary ClassLoader for type matching.
        beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    }
    // Register default environment beans.
    if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
    }
}
Also used : LoadTimeWeaverAwareProcessor(org.springframework.context.weaving.LoadTimeWeaverAwareProcessor) ResourceEditorRegistrar(org.springframework.beans.support.ResourceEditorRegistrar) StandardBeanExpressionResolver(org.springframework.context.expression.StandardBeanExpressionResolver)

Example 2 with StandardBeanExpressionResolver

use of org.springframework.context.expression.StandardBeanExpressionResolver 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)

Example 3 with StandardBeanExpressionResolver

use of org.springframework.context.expression.StandardBeanExpressionResolver in project spring-framework by spring-projects.

the class RequestScopeTests method setup.

@Before
public void setup() throws Exception {
    this.beanFactory.registerScope("request", new RequestScope());
    this.beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
    reader.loadBeanDefinitions(new ClassPathResource("requestScopeTests.xml", getClass()));
    this.beanFactory.preInstantiateSingletons();
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) StandardBeanExpressionResolver(org.springframework.context.expression.StandardBeanExpressionResolver) ClassPathResource(org.springframework.core.io.ClassPathResource) Before(org.junit.Before)

Aggregations

StandardBeanExpressionResolver (org.springframework.context.expression.StandardBeanExpressionResolver)3 Before (org.junit.Before)1 BeanExpressionContext (org.springframework.beans.factory.config.BeanExpressionContext)1 BeanExpressionResolver (org.springframework.beans.factory.config.BeanExpressionResolver)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1 ResourceEditorRegistrar (org.springframework.beans.support.ResourceEditorRegistrar)1 LoadTimeWeaverAwareProcessor (org.springframework.context.weaving.LoadTimeWeaverAwareProcessor)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1