From 9fb9a67c4bc895ead0008db106145953d3b3b855 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 3 Mar 2017 16:38:58 +0100 Subject: [PATCH] Avoid early initializations This commit flags the two `BeanPostProcessors` registered by the embedded support as `synthetic` so that they don't trigger an early initialization of other components. Closes gh-8467 --- ...ddedServletContainerAutoConfiguration.java | 15 ++++++++++----- ...tContainerCustomizerBeanPostProcessor.java | 19 ++++++++++--------- .../ErrorPageRegistrarBeanPostProcessor.java | 19 ++++++++++--------- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration.java index 4c2ab7cfd9..e04fff99cd 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -58,6 +58,7 @@ import org.springframework.util.ObjectUtils; * @author Phillip Webb * @author Dave Syer * @author Ivan Sopov + * @author Stephane Nicoll */ @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @Configuration @@ -136,17 +137,21 @@ public class EmbeddedServletContainerAutoConfiguration { if (ObjectUtils.isEmpty(this.beanFactory.getBeanNamesForType( EmbeddedServletContainerCustomizerBeanPostProcessor.class, true, false))) { + RootBeanDefinition beanDefinition = new RootBeanDefinition( + EmbeddedServletContainerCustomizerBeanPostProcessor.class); + beanDefinition.setSynthetic(true); registry.registerBeanDefinition( "embeddedServletContainerCustomizerBeanPostProcessor", - new RootBeanDefinition( - EmbeddedServletContainerCustomizerBeanPostProcessor.class)); + beanDefinition); } if (ObjectUtils.isEmpty(this.beanFactory.getBeanNamesForType( ErrorPageRegistrarBeanPostProcessor.class, true, false))) { + RootBeanDefinition beanDefinition = new RootBeanDefinition( + ErrorPageRegistrarBeanPostProcessor.class); + beanDefinition.setSynthetic(true); registry.registerBeanDefinition("errorPageRegistrarBeanPostProcessor", - new RootBeanDefinition( - ErrorPageRegistrarBeanPostProcessor.class)); + beanDefinition); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerCustomizerBeanPostProcessor.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerCustomizerBeanPostProcessor.java index 6e38d07492..e4b41309db 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerCustomizerBeanPostProcessor.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerCustomizerBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2017 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. @@ -22,9 +22,10 @@ import java.util.Collections; import java.util.List; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; import org.springframework.core.annotation.AnnotationAwareOrderComparator; /** @@ -33,18 +34,18 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; * * @author Dave Syer * @author Phillip Webb + * @author Stephane Nicoll */ public class EmbeddedServletContainerCustomizerBeanPostProcessor - implements BeanPostProcessor, ApplicationContextAware { + implements BeanPostProcessor, BeanFactoryAware { - private ApplicationContext applicationContext; + private ListableBeanFactory beanFactory; private List customizers; @Override - public void setApplicationContext(ApplicationContext applicationContext) - throws BeansException { - this.applicationContext = applicationContext; + public void setBeanFactory(BeanFactory beanFactory) { + this.beanFactory = (ListableBeanFactory) beanFactory; } @Override @@ -73,7 +74,7 @@ public class EmbeddedServletContainerCustomizerBeanPostProcessor if (this.customizers == null) { // Look up does not include the parent context this.customizers = new ArrayList( - this.applicationContext + this.beanFactory .getBeansOfType(EmbeddedServletContainerCustomizer.class, false, false) .values()); diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ErrorPageRegistrarBeanPostProcessor.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ErrorPageRegistrarBeanPostProcessor.java index fa476f32b9..292a096a89 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/ErrorPageRegistrarBeanPostProcessor.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/ErrorPageRegistrarBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -22,9 +22,10 @@ import java.util.Collections; import java.util.List; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; import org.springframework.core.annotation.AnnotationAwareOrderComparator; /** @@ -32,19 +33,19 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; * factory to {@link ErrorPageRegistry} beans. * * @author Phillip Webb + * @author Stephane Nicoll * @since 1.4.0 */ public class ErrorPageRegistrarBeanPostProcessor - implements BeanPostProcessor, ApplicationContextAware { + implements BeanPostProcessor, BeanFactoryAware { - private ApplicationContext applicationContext; + private ListableBeanFactory beanFactory; private List registrars; @Override - public void setApplicationContext(ApplicationContext applicationContext) - throws BeansException { - this.applicationContext = applicationContext; + public void setBeanFactory(BeanFactory beanFactory) { + this.beanFactory = (ListableBeanFactory) beanFactory; } @Override @@ -71,7 +72,7 @@ public class ErrorPageRegistrarBeanPostProcessor private Collection getRegistrars() { if (this.registrars == null) { // Look up does not include the parent context - this.registrars = new ArrayList(this.applicationContext + this.registrars = new ArrayList(this.beanFactory .getBeansOfType(ErrorPageRegistrar.class, false, false).values()); Collections.sort(this.registrars, AnnotationAwareOrderComparator.INSTANCE); this.registrars = Collections.unmodifiableList(this.registrars);