diff --git a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java index 9b6a0619a1a..0663b9e6057 100644 --- a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java +++ b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -142,7 +142,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe /** * Initialize this instance. */ - void init(ApplicationContext applicationContext, EventExpressionEvaluator evaluator) { + void init(ApplicationContext applicationContext, @Nullable EventExpressionEvaluator evaluator) { this.applicationContext = applicationContext; this.evaluator = evaluator; } diff --git a/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java b/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java index 07cedeee7a1..ad89b9e5676 100644 --- a/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +40,7 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.MethodIntrospector; +import org.springframework.core.SpringProperties; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationUtils; @@ -56,6 +57,7 @@ import org.springframework.util.CollectionUtils; * * @author Stephane Nicoll * @author Juergen Hoeller + * @author Sebastien Deleuze * @since 4.2 * @see EventListenerFactory * @see DefaultEventListenerFactory @@ -63,6 +65,14 @@ import org.springframework.util.CollectionUtils; public class EventListenerMethodProcessor implements SmartInitializingSingleton, ApplicationContextAware, BeanFactoryPostProcessor { + /** + * Boolean flag controlled by a {@code spring.spel.ignore} system property that instructs Spring to + * ignore SpEL, i.e. to not initialize the SpEL infrastructure. + *

The default is "false". + */ + private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore"); + + protected final Log logger = LogFactory.getLog(getClass()); @Nullable @@ -74,11 +84,21 @@ public class EventListenerMethodProcessor @Nullable private List eventListenerFactories; - private final EventExpressionEvaluator evaluator = new EventExpressionEvaluator(); + @Nullable + private final EventExpressionEvaluator evaluator; private final Set> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64)); + public EventListenerMethodProcessor() { + if (shouldIgnoreSpel) { + this.evaluator = null; + } + else { + this.evaluator = new EventExpressionEvaluator(); + } + } + @Override public void setApplicationContext(ApplicationContext applicationContext) { Assert.isTrue(applicationContext instanceof ConfigurableApplicationContext, diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java index 933fc7359cb..13c635c668c 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java @@ -67,6 +67,7 @@ import org.springframework.context.expression.StandardBeanExpressionResolver; import org.springframework.context.weaving.LoadTimeWeaverAware; import org.springframework.context.weaving.LoadTimeWeaverAwareProcessor; import org.springframework.core.ResolvableType; +import org.springframework.core.SpringProperties; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.convert.ConversionService; import org.springframework.core.env.ConfigurableEnvironment; @@ -117,6 +118,7 @@ import org.springframework.util.ReflectionUtils; * @author Mark Fisher * @author Stephane Nicoll * @author Sam Brannen + * @author Sebastien Deleuze * @since January 21, 2001 * @see #refreshBeanFactory * @see #getBeanFactory @@ -152,6 +154,13 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader */ public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster"; + /** + * Boolean flag controlled by a {@code spring.spel.ignore} system property that instructs Spring to + * ignore SpEL, i.e. to not initialize the SpEL infrastructure. + *

The default is "false". + */ + private static final boolean shouldIgnoreSpel = SpringProperties.getFlag("spring.spel.ignore"); + static { // Eagerly load the ContextClosedEvent class to avoid weird classloader issues @@ -647,7 +656,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader 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())); + if (!shouldIgnoreSpel) { + beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); + } beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); // Configure the bean factory with context callbacks.