|
17 | 17 | package org.springframework.boot.actuate.autoconfigure;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.lang.reflect.Modifier; |
20 | 21 |
|
21 | 22 | import javax.servlet.FilterChain;
|
22 | 23 | import javax.servlet.Servlet;
|
|
28 | 29 | import org.apache.commons.logging.LogFactory;
|
29 | 30 |
|
30 | 31 | import org.springframework.beans.BeansException;
|
| 32 | +import org.springframework.beans.FatalBeanException; |
31 | 33 | import org.springframework.beans.factory.BeanFactory;
|
32 | 34 | import org.springframework.beans.factory.BeanFactoryAware;
|
33 | 35 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
@@ -188,20 +190,38 @@ private void createChildManagementContext() {
|
188 | 190 | private void registerEmbeddedServletContainerFactory(
|
189 | 191 | AnnotationConfigEmbeddedWebApplicationContext childContext) {
|
190 | 192 | try {
|
191 |
| - EmbeddedServletContainerFactory servletContainerFactory = this.applicationContext |
192 |
| - .getBean(EmbeddedServletContainerFactory.class); |
193 | 193 | ConfigurableListableBeanFactory beanFactory = childContext.getBeanFactory();
|
194 | 194 | if (beanFactory instanceof BeanDefinitionRegistry) {
|
195 | 195 | BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
|
196 | 196 | registry.registerBeanDefinition("embeddedServletContainerFactory",
|
197 |
| - new RootBeanDefinition(servletContainerFactory.getClass())); |
| 197 | + new RootBeanDefinition( |
| 198 | + determineEmbeddedServletContainerFactoryClass())); |
198 | 199 | }
|
199 | 200 | }
|
200 | 201 | catch (NoSuchBeanDefinitionException ex) {
|
201 | 202 | // Ignore and assume auto-configuration
|
202 | 203 | }
|
203 | 204 | }
|
204 | 205 |
|
| 206 | + private Class<?> determineEmbeddedServletContainerFactoryClass() |
| 207 | + throws NoSuchBeanDefinitionException { |
| 208 | + Class<?> servletContainerFactoryClass = this.applicationContext |
| 209 | + .getBean(EmbeddedServletContainerFactory.class).getClass(); |
| 210 | + if (cannotBeInstantiated(servletContainerFactoryClass)) { |
| 211 | + throw new FatalBeanException("EmbeddedServletContainerFactory implementation " |
| 212 | + + servletContainerFactoryClass.getName() + " cannot be instantiated. " |
| 213 | + + "To allow a separate management port to be used, a top-level class " |
| 214 | + + "or static inner class should be used instead"); |
| 215 | + } |
| 216 | + return servletContainerFactoryClass; |
| 217 | + } |
| 218 | + |
| 219 | + private boolean cannotBeInstantiated(Class<?> clazz) { |
| 220 | + return clazz.isLocalClass() |
| 221 | + || (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) |
| 222 | + || clazz.isAnonymousClass(); |
| 223 | + } |
| 224 | + |
205 | 225 | /**
|
206 | 226 | * Add an alias for 'local.management.port' that actually resolves using
|
207 | 227 | * 'local.server.port'.
|
|
0 commit comments