mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Use enum for parent context modes in @ConditionalOnBean
@ConditionOn*Bean now have search=ALL by default (CURRENT and PARENTS also supported). [Fixes #53538357]
This commit is contained in:
parent
b02be6e6ec
commit
7dd186aa40
@ -26,6 +26,7 @@ import org.springframework.beans.factory.HierarchicalBeanFactory;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.bootstrap.context.condition.ConditionalOnBean;
|
import org.springframework.bootstrap.context.condition.ConditionalOnBean;
|
||||||
import org.springframework.bootstrap.context.condition.ConditionalOnClass;
|
import org.springframework.bootstrap.context.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.bootstrap.context.condition.SearchStrategy;
|
||||||
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainer;
|
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainer;
|
||||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
|
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
|
||||||
@ -81,7 +82,7 @@ public class EndpointWebMvcChildContextConfiguration implements
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ EnableWebSecurity.class, Filter.class })
|
@ConditionalOnClass({ EnableWebSecurity.class, Filter.class })
|
||||||
@ConditionalOnBean(name = "springSecurityFilterChain", parentOnly = true)
|
@ConditionalOnBean(name = "springSecurityFilterChain", search = SearchStrategy.PARENTS)
|
||||||
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
|
||||||
|
@ -24,6 +24,7 @@ import org.eclipse.jetty.util.Loader;
|
|||||||
import org.springframework.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.bootstrap.context.condition.ConditionalOnClass;
|
import org.springframework.bootstrap.context.condition.ConditionalOnClass;
|
||||||
import org.springframework.bootstrap.context.condition.ConditionalOnMissingBean;
|
import org.springframework.bootstrap.context.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.bootstrap.context.condition.SearchStrategy;
|
||||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
|
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
|
||||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
||||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
|
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
|
||||||
@ -50,7 +51,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
|||||||
* {@link EmbeddedServletContainerCustomizer}s.
|
* {@link EmbeddedServletContainerCustomizer}s.
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerCustomizerBeanPostProcessor.class, parentContext = false)
|
@ConditionalOnMissingBean(value = EmbeddedServletContainerCustomizerBeanPostProcessor.class, search = SearchStrategy.CURRENT)
|
||||||
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() {
|
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() {
|
||||||
return new EmbeddedServletContainerCustomizerBeanPostProcessor();
|
return new EmbeddedServletContainerCustomizerBeanPostProcessor();
|
||||||
}
|
}
|
||||||
@ -64,7 +65,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(value = { ServletContextInitializer.class,
|
@ConditionalOnMissingBean(value = { ServletContextInitializer.class,
|
||||||
Servlet.class }, parentContext = false)
|
Servlet.class }, search = SearchStrategy.CURRENT)
|
||||||
public DispatcherServlet dispatcherServlet() {
|
public DispatcherServlet dispatcherServlet() {
|
||||||
return new DispatcherServlet();
|
return new DispatcherServlet();
|
||||||
}
|
}
|
||||||
@ -75,7 +76,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ Servlet.class, Tomcat.class })
|
@ConditionalOnClass({ Servlet.class, Tomcat.class })
|
||||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, parentContext = false)
|
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
|
||||||
public static class EmbeddedTomcat {
|
public static class EmbeddedTomcat {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -90,7 +91,7 @@ public class EmbeddedServletContainerAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class })
|
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class })
|
||||||
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, parentContext = false)
|
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
|
||||||
public static class EmbeddedJetty {
|
public static class EmbeddedJetty {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -98,13 +98,12 @@ abstract class AbstractOnBeanCondition implements ConfigurationCondition {
|
|||||||
|
|
||||||
String checking = ConditionLogUtils.getPrefix(this.logger, metadata);
|
String checking = ConditionLogUtils.getPrefix(this.logger, metadata);
|
||||||
|
|
||||||
Boolean considerHierarchy = (Boolean) metadata.getAnnotationAttributes(
|
SearchStrategy search = (SearchStrategy) metadata.getAnnotationAttributes(
|
||||||
annotationClass().getName()).get("parentContext");
|
annotationClass().getName()).get("search");
|
||||||
considerHierarchy = (considerHierarchy == null ? false : considerHierarchy);
|
search = search == null ? SearchStrategy.ALL : search;
|
||||||
|
|
||||||
Boolean parentOnly = (Boolean) metadata.getAnnotationAttributes(
|
boolean considerHierarchy = search == SearchStrategy.ALL;
|
||||||
annotationClass().getName()).get("parentOnly");
|
boolean parentOnly = search == SearchStrategy.PARENTS;
|
||||||
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>();
|
||||||
|
@ -53,13 +53,9 @@ public @interface ConditionalOnBean {
|
|||||||
String[] name() default {};
|
String[] name() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the application context hierarchy (parent contexts) should be considered.
|
* Strategy to decide if the application context hierarchy (parent contexts) should be
|
||||||
|
* considered.
|
||||||
*/
|
*/
|
||||||
boolean parentContext() default true;
|
SearchStrategy search() default SearchStrategy.ALL;
|
||||||
|
|
||||||
/**
|
|
||||||
* If only the application parent contexts should be considered.
|
|
||||||
*/
|
|
||||||
boolean parentOnly() default false;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -53,8 +53,9 @@ public @interface ConditionalOnMissingBean {
|
|||||||
String[] name() default {};
|
String[] name() default {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the application context hierarchy (parent contexts) should be considered.
|
* Strategy to decide if the application context hierarchy (parent contexts) should be
|
||||||
|
* considered.
|
||||||
*/
|
*/
|
||||||
boolean parentContext() default true;
|
SearchStrategy search() default SearchStrategy.ALL;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2013 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.bootstrap.context.condition;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Some named search strategies for beans in the bean factory hierarchy.
|
||||||
|
*
|
||||||
|
* @author Dave Syer
|
||||||
|
*/
|
||||||
|
public enum SearchStrategy {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search only the current context
|
||||||
|
*/
|
||||||
|
CURRENT,
|
||||||
|
/**
|
||||||
|
* Search all parents and ancestors, but not the current context
|
||||||
|
*/
|
||||||
|
PARENTS,
|
||||||
|
/**
|
||||||
|
* Search the entire hierarchy
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
ALL;
|
||||||
|
|
||||||
|
}
|
@ -111,7 +111,7 @@ public class OnMissingBeanConditionTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnMissingBean(name = "foo", parentContext = false)
|
@ConditionalOnMissingBean(name = "foo", search = SearchStrategy.CURRENT)
|
||||||
protected static class HierarchyNotConsidered {
|
protected static class HierarchyNotConsidered {
|
||||||
@Bean
|
@Bean
|
||||||
public String bar() {
|
public String bar() {
|
||||||
|
Loading…
Reference in New Issue
Block a user