mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
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
|
||||
@ConditionalOnClass({ EnableWebSecurity.class, Filter.class })
|
||||
@ConditionalOnBean(name = "springSecurityFilterChain", parentOnly = true)
|
||||
public static class EndpointWebMvcChildContextSecurityConfiguration {
|
||||
|
||||
// FIXME reuse of security filter here is not good. What if totally different
|
||||
@ -88,7 +89,6 @@ public class EndpointWebMvcChildContextConfiguration implements
|
||||
// port?
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(name = "springSecurityFilterChain")
|
||||
public Filter springSecurityFilterChain(HierarchicalBeanFactory beanFactory) {
|
||||
BeanFactory parent = beanFactory.getParentBeanFactory();
|
||||
return parent.getBean("springSecurityFilterChain", Filter.class);
|
||||
|
@ -22,7 +22,6 @@ import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.actuate.endpoint.AbstractEndpoint;
|
||||
import org.springframework.actuate.endpoint.Endpoint;
|
||||
@ -84,8 +83,6 @@ public class EndpointWebMvcAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
// FIXME: this broke recently
|
||||
public void onDifferentPort() throws Exception {
|
||||
this.applicationContext.register(RootConfig.class, DifferentPortConfig.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
@ -120,8 +117,6 @@ public class EndpointWebMvcAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
// FIXME: this broke recently
|
||||
public void specificPortsViaProperties() throws Exception {
|
||||
TestUtils.addEnviroment(this.applicationContext, "server.port:7070",
|
||||
"management.port:7071");
|
||||
|
@ -50,7 +50,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||
* {@link EmbeddedServletContainerCustomizer}s.
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerCustomizerBeanPostProcessor.class, considerHierarchy = false)
|
||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerCustomizerBeanPostProcessor.class, parentContext = false)
|
||||
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() {
|
||||
return new EmbeddedServletContainerCustomizerBeanPostProcessor();
|
||||
}
|
||||
@ -64,7 +64,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(value = { ServletContextInitializer.class,
|
||||
Servlet.class }, considerHierarchy = false)
|
||||
Servlet.class }, parentContext = false)
|
||||
public DispatcherServlet dispatcherServlet() {
|
||||
return new DispatcherServlet();
|
||||
}
|
||||
@ -75,7 +75,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, Tomcat.class })
|
||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, considerHierarchy = false)
|
||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, parentContext = false)
|
||||
public static class EmbeddedTomcat {
|
||||
|
||||
@Bean
|
||||
@ -90,7 +90,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class })
|
||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, considerHierarchy = false)
|
||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, parentContext = false)
|
||||
public static class EmbeddedJetty {
|
||||
|
||||
@Bean
|
||||
|
@ -22,6 +22,7 @@ import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@ -98,18 +99,30 @@ abstract class AbstractOnBeanCondition implements ConfigurationCondition {
|
||||
String checking = ConditionLogUtils.getPrefix(this.logger, metadata);
|
||||
|
||||
Boolean considerHierarchy = (Boolean) metadata.getAnnotationAttributes(
|
||||
annotationClass().getName()).get("considerHierarchy");
|
||||
annotationClass().getName()).get("parentContext");
|
||||
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> 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) {
|
||||
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());
|
||||
String[] beans = (considerHierarchy ? BeanFactoryUtils
|
||||
.beanNamesForTypeIncludingAncestors(beanFactory, type, false,
|
||||
@ -124,8 +137,8 @@ abstract class AbstractOnBeanCondition implements ConfigurationCondition {
|
||||
}
|
||||
}
|
||||
for (String beanName : beanNames) {
|
||||
if (considerHierarchy ? context.getBeanFactory().containsBean(beanName)
|
||||
: context.getBeanFactory().containsLocalBean(beanName)) {
|
||||
if (considerHierarchy ? beanFactory.containsBean(beanName) : beanFactory
|
||||
.containsLocalBean(beanName)) {
|
||||
beanNamesFound.add(beanName);
|
||||
}
|
||||
}
|
||||
|
@ -55,6 +55,11 @@ public @interface ConditionalOnBean {
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
boolean considerHierarchy() default true;
|
||||
boolean parentContext() default true;
|
||||
|
||||
}
|
||||
|
@ -17,8 +17,6 @@
|
||||
package org.springframework.bootstrap.context.condition;
|
||||
|
||||
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.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -113,7 +111,7 @@ public class OnMissingBeanConditionTests {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(name = "foo", considerHierarchy = false)
|
||||
@ConditionalOnMissingBean(name = "foo", parentContext = false)
|
||||
protected static class HierarchyNotConsidered {
|
||||
@Bean
|
||||
public String bar() {
|
||||
|
Loading…
Reference in New Issue
Block a user