|
22 | 22 | import org.eclipse.jetty.server.Server;
|
23 | 23 | import org.eclipse.jetty.util.Loader;
|
24 | 24 | import org.springframework.autoconfigure.EnableAutoConfiguration;
|
| 25 | +import org.springframework.autoconfigure.web.EmbeddedServletContainerAutoConfiguration.EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar; |
| 26 | +import org.springframework.beans.BeansException; |
| 27 | +import org.springframework.beans.factory.BeanFactory; |
| 28 | +import org.springframework.beans.factory.BeanFactoryAware; |
| 29 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 30 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 31 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 32 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
25 | 33 | import org.springframework.bootstrap.context.condition.ConditionalOnClass;
|
26 | 34 | import org.springframework.bootstrap.context.condition.ConditionalOnMissingBean;
|
27 | 35 | import org.springframework.bootstrap.context.condition.SearchStrategy;
|
28 |
| -import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer; |
29 | 36 | import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
30 | 37 | import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
|
31 | 38 | import org.springframework.bootstrap.context.embedded.ServletContextInitializer;
|
32 | 39 | import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
|
33 | 40 | import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
34 | 41 | import org.springframework.context.annotation.Bean;
|
35 | 42 | import org.springframework.context.annotation.Configuration;
|
| 43 | +import org.springframework.context.annotation.Import; |
| 44 | +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
36 | 45 | import org.springframework.core.Ordered;
|
37 | 46 | import org.springframework.core.annotation.Order;
|
| 47 | +import org.springframework.core.type.AnnotationMetadata; |
38 | 48 | import org.springframework.web.servlet.DispatcherServlet;
|
39 | 49 |
|
40 | 50 | /**
|
|
44 | 54 | * @author Dave Syer
|
45 | 55 | */
|
46 | 56 | @Order(Ordered.HIGHEST_PRECEDENCE)
|
| 57 | +@Configuration |
| 58 | +@Import(EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class) |
47 | 59 | public class EmbeddedServletContainerAutoConfiguration {
|
48 | 60 |
|
49 |
| - /** |
50 |
| - * Support {@link EmbeddedServletContainerCustomizerBeanPostProcessor} to apply |
51 |
| - * {@link EmbeddedServletContainerCustomizer}s. |
52 |
| - */ |
53 |
| - @Bean |
54 |
| - @ConditionalOnMissingBean(value = EmbeddedServletContainerCustomizerBeanPostProcessor.class, search = SearchStrategy.CURRENT) |
55 |
| - public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() { |
56 |
| - return new EmbeddedServletContainerCustomizerBeanPostProcessor(); |
57 |
| - } |
58 |
| - |
59 | 61 | /**
|
60 | 62 | * Add the {@link DispatcherServlet} unless the user has defined their own
|
61 | 63 | * {@link ServletContextInitializer}s.
|
@@ -101,4 +103,35 @@ public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory
|
101 | 103 |
|
102 | 104 | }
|
103 | 105 |
|
| 106 | + /** |
| 107 | + * Registers a {@link EmbeddedServletContainerCustomizerBeanPostProcessor}. Registered |
| 108 | + * via {@link ImportBeanDefinitionRegistrar} for early registration. |
| 109 | + */ |
| 110 | + public static class EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar |
| 111 | + implements ImportBeanDefinitionRegistrar, BeanFactoryAware { |
| 112 | + |
| 113 | + private ConfigurableListableBeanFactory beanFactory; |
| 114 | + |
| 115 | + @Override |
| 116 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 117 | + if (beanFactory instanceof ConfigurableListableBeanFactory) { |
| 118 | + this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, |
| 124 | + BeanDefinitionRegistry registry) { |
| 125 | + if (this.beanFactory != null |
| 126 | + && this.beanFactory.getBeansOfType( |
| 127 | + EmbeddedServletContainerCustomizerBeanPostProcessor.class) |
| 128 | + .size() == 0) { |
| 129 | + BeanDefinition beanDefinition = new RootBeanDefinition( |
| 130 | + EmbeddedServletContainerCustomizerBeanPostProcessor.class); |
| 131 | + registry.registerBeanDefinition( |
| 132 | + "embeddedServletContainerCustomizerBeanPostProcessor", |
| 133 | + beanDefinition); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
104 | 137 | }
|
0 commit comments