Skip to content

Commit

Permalink
Provide a flag to disable SpEL support
Browse files Browse the repository at this point in the history
This commit introduces a spring.spel.ignore flag which
when set to true avoid initializing SpEL infrastructure.

A typical use case is optimizing GraalVM native image footprint.

Closes spring-projectsgh-25153
  • Loading branch information
sdeleuze committed Jun 18, 2020
1 parent 6b355df commit d30a5eb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static int resolveOrder(Method method) {
/**
* Initialize this instance.
*/
void init(ApplicationContext applicationContext, EventExpressionEvaluator evaluator) {
void init(ApplicationContext applicationContext, @Nullable EventExpressionEvaluator evaluator) {
this.applicationContext = applicationContext;
this.evaluator = evaluator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
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;
Expand All @@ -63,6 +65,9 @@
public class EventListenerMethodProcessor
implements SmartInitializingSingleton, ApplicationContextAware, BeanFactoryPostProcessor {

private static final boolean shouldIgnoreSpel =
SpringProperties.getFlag(AbstractApplicationContext.IGNORE_SPEL_PROPERTY_NAME);

protected final Log logger = LogFactory.getLog(getClass());

@Nullable
Expand All @@ -74,11 +79,18 @@ public class EventListenerMethodProcessor
@Nullable
private List<EventListenerFactory> eventListenerFactories;

private final EventExpressionEvaluator evaluator = new EventExpressionEvaluator();
@Nullable
private EventExpressionEvaluator evaluator;

private final Set<Class<?>> nonAnnotatedClasses = Collections.newSetFromMap(new ConcurrentHashMap<>(64));


public EventListenerMethodProcessor() {
if (!shouldIgnoreSpel) {
this.evaluator = new EventExpressionEvaluator();
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
Assert.isTrue(applicationContext instanceof ConfigurableApplicationContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
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;
Expand Down Expand Up @@ -152,6 +153,15 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
*/
public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";

/**
* System property that instructs Spring to ignore SpEL, i.e.
* to not initialize the SpEL infrastructure.
* <p>The default is "false".
*/
public static final String IGNORE_SPEL_PROPERTY_NAME = "spring.spel.ignore";

private static final boolean shouldIgnoreSpel = SpringProperties.getFlag(IGNORE_SPEL_PROPERTY_NAME);


static {
// Eagerly load the ContextClosedEvent class to avoid weird classloader issues
Expand Down Expand Up @@ -647,7 +657,9 @@ protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
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.
Expand Down

0 comments on commit d30a5eb

Please sign in to comment.