Add option in @OnBeanCondition to *only* look in parent
[Fixes #53374039] [bs-231] Broken tests for management port configuration
This commit is contained in:
parent
789e75d2e5
commit
f52d624be5
|
|
@ -81,6 +81,7 @@ public class EndpointWebMvcChildContextConfiguration implements
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ EnableWebSecurity.class, Filter.class })
|
@ConditionalOnClass({ EnableWebSecurity.class, Filter.class })
|
||||||
|
@ConditionalOnBean(name = "springSecurityFilterChain", parentOnly = true)
|
||||||
public static class EndpointWebMvcChildContextSecurityConfiguration {
|
public static class EndpointWebMvcChildContextSecurityConfiguration {
|
||||||
|
|
||||||
// FIXME reuse of security filter here is not good. What if totally different
|
// FIXME reuse of security filter here is not good. What if totally different
|
||||||
|
|
@ -88,7 +89,6 @@ public class EndpointWebMvcChildContextConfiguration implements
|
||||||
// port?
|
// port?
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnBean(name = "springSecurityFilterChain")
|
|
||||||
public Filter springSecurityFilterChain(HierarchicalBeanFactory beanFactory) {
|
public Filter springSecurityFilterChain(HierarchicalBeanFactory beanFactory) {
|
||||||
BeanFactory parent = beanFactory.getParentBeanFactory();
|
BeanFactory parent = beanFactory.getParentBeanFactory();
|
||||||
return parent.getBean("springSecurityFilterChain", Filter.class);
|
return parent.getBean("springSecurityFilterChain", Filter.class);
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,6 @@ import java.net.URI;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.actuate.endpoint.AbstractEndpoint;
|
import org.springframework.actuate.endpoint.AbstractEndpoint;
|
||||||
import org.springframework.actuate.endpoint.Endpoint;
|
import org.springframework.actuate.endpoint.Endpoint;
|
||||||
|
|
@ -84,8 +83,6 @@ public class EndpointWebMvcAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
// FIXME: this broke recently
|
|
||||||
public void onDifferentPort() throws Exception {
|
public void onDifferentPort() throws Exception {
|
||||||
this.applicationContext.register(RootConfig.class, DifferentPortConfig.class,
|
this.applicationContext.register(RootConfig.class, DifferentPortConfig.class,
|
||||||
PropertyPlaceholderAutoConfiguration.class,
|
PropertyPlaceholderAutoConfiguration.class,
|
||||||
|
|
@ -120,8 +117,6 @@ public class EndpointWebMvcAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
// FIXME: this broke recently
|
|
||||||
public void specificPortsViaProperties() throws Exception {
|
public void specificPortsViaProperties() throws Exception {
|
||||||
TestUtils.addEnviroment(this.applicationContext, "server.port:7070",
|
TestUtils.addEnviroment(this.applicationContext, "server.port:7070",
|
||||||
"management.port:7071");
|
"management.port:7071");
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||||
* {@link EmbeddedServletContainerCustomizer}s.
|
* {@link EmbeddedServletContainerCustomizer}s.
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerCustomizerBeanPostProcessor.class, considerHierarchy = false)
|
@ConditionalOnMissingBean(value = EmbeddedServletContainerCustomizerBeanPostProcessor.class, parentContext = false)
|
||||||
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() {
|
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() {
|
||||||
return new EmbeddedServletContainerCustomizerBeanPostProcessor();
|
return new EmbeddedServletContainerCustomizerBeanPostProcessor();
|
||||||
}
|
}
|
||||||
|
|
@ -64,7 +64,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(value = { ServletContextInitializer.class,
|
@ConditionalOnMissingBean(value = { ServletContextInitializer.class,
|
||||||
Servlet.class }, considerHierarchy = false)
|
Servlet.class }, parentContext = false)
|
||||||
public DispatcherServlet dispatcherServlet() {
|
public DispatcherServlet dispatcherServlet() {
|
||||||
return new DispatcherServlet();
|
return new DispatcherServlet();
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +75,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ Servlet.class, Tomcat.class })
|
@ConditionalOnClass({ Servlet.class, Tomcat.class })
|
||||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, considerHierarchy = false)
|
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, parentContext = false)
|
||||||
public static class EmbeddedTomcat {
|
public static class EmbeddedTomcat {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
@ -90,7 +90,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class })
|
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class })
|
||||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, considerHierarchy = false)
|
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, parentContext = false)
|
||||||
public static class EmbeddedJetty {
|
public static class EmbeddedJetty {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
@ -98,18 +99,30 @@ abstract class AbstractOnBeanCondition implements ConfigurationCondition {
|
||||||
String checking = ConditionLogUtils.getPrefix(this.logger, metadata);
|
String checking = ConditionLogUtils.getPrefix(this.logger, metadata);
|
||||||
|
|
||||||
Boolean considerHierarchy = (Boolean) metadata.getAnnotationAttributes(
|
Boolean considerHierarchy = (Boolean) metadata.getAnnotationAttributes(
|
||||||
annotationClass().getName()).get("considerHierarchy");
|
annotationClass().getName()).get("parentContext");
|
||||||
considerHierarchy = (considerHierarchy == null ? false : considerHierarchy);
|
considerHierarchy = (considerHierarchy == null ? false : considerHierarchy);
|
||||||
|
|
||||||
|
Boolean parentOnly = (Boolean) metadata.getAnnotationAttributes(
|
||||||
|
annotationClass().getName()).get("parentOnly");
|
||||||
|
parentOnly = (parentOnly == null ? false : parentOnly);
|
||||||
|
|
||||||
List<String> beanClassesFound = new ArrayList<String>();
|
List<String> beanClassesFound = new ArrayList<String>();
|
||||||
List<String> beanNamesFound = new ArrayList<String>();
|
List<String> beanNamesFound = new ArrayList<String>();
|
||||||
|
|
||||||
|
// eagerInit set to false to prevent early instantiation (some
|
||||||
|
// factory beans will not be able to determine their object type at this
|
||||||
|
// stage, so those are not eligible for matching this condition)
|
||||||
|
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
|
||||||
|
if (parentOnly) {
|
||||||
|
BeanFactory parent = beanFactory.getParentBeanFactory();
|
||||||
|
if (!(parent instanceof ConfigurableListableBeanFactory)) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"Cannot use parentOnly if parent is not ConfigurableListableBeanFactory");
|
||||||
|
}
|
||||||
|
beanFactory = (ConfigurableListableBeanFactory) parent;
|
||||||
|
}
|
||||||
for (String beanClass : beanClasses) {
|
for (String beanClass : beanClasses) {
|
||||||
try {
|
try {
|
||||||
// eagerInit set to false to prevent early instantiation (some
|
|
||||||
// factory beans will not be able to determine their object type at this
|
|
||||||
// stage, so those are not eligible for matching this condition)
|
|
||||||
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
|
|
||||||
Class<?> type = ClassUtils.forName(beanClass, context.getClassLoader());
|
Class<?> type = ClassUtils.forName(beanClass, context.getClassLoader());
|
||||||
String[] beans = (considerHierarchy ? BeanFactoryUtils
|
String[] beans = (considerHierarchy ? BeanFactoryUtils
|
||||||
.beanNamesForTypeIncludingAncestors(beanFactory, type, false,
|
.beanNamesForTypeIncludingAncestors(beanFactory, type, false,
|
||||||
|
|
@ -124,8 +137,8 @@ abstract class AbstractOnBeanCondition implements ConfigurationCondition {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (String beanName : beanNames) {
|
for (String beanName : beanNames) {
|
||||||
if (considerHierarchy ? context.getBeanFactory().containsBean(beanName)
|
if (considerHierarchy ? beanFactory.containsBean(beanName) : beanFactory
|
||||||
: context.getBeanFactory().containsLocalBean(beanName)) {
|
.containsLocalBean(beanName)) {
|
||||||
beanNamesFound.add(beanName);
|
beanNamesFound.add(beanName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,11 @@ public @interface ConditionalOnBean {
|
||||||
/**
|
/**
|
||||||
* If the application context hierarchy (parent contexts) should be considered.
|
* If the application context hierarchy (parent contexts) should be considered.
|
||||||
*/
|
*/
|
||||||
boolean considerHierarchy() default true;
|
boolean parentContext() default true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If only the application parent contexts should be considered.
|
||||||
|
*/
|
||||||
|
boolean parentOnly() default false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,6 @@ public @interface ConditionalOnMissingBean {
|
||||||
/**
|
/**
|
||||||
* If the application context hierarchy (parent contexts) should be considered.
|
* If the application context hierarchy (parent contexts) should be considered.
|
||||||
*/
|
*/
|
||||||
boolean considerHierarchy() default true;
|
boolean parentContext() default true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,6 @@
|
||||||
package org.springframework.bootstrap.context.condition;
|
package org.springframework.bootstrap.context.condition;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.bootstrap.context.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.bootstrap.context.condition.OnMissingBeanCondition;
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -113,7 +111,7 @@ public class OnMissingBeanConditionTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnMissingBean(name = "foo", considerHierarchy = false)
|
@ConditionalOnMissingBean(name = "foo", parentContext = false)
|
||||||
protected static class HierarchyNotConsidered {
|
protected static class HierarchyNotConsidered {
|
||||||
@Bean
|
@Bean
|
||||||
public String bar() {
|
public String bar() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue