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:
Dave Syer 2013-07-17 12:25:54 +01:00
parent b02be6e6ec
commit 7dd186aa40
7 changed files with 59 additions and 21 deletions

View File

@ -26,6 +26,7 @@ import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.bootstrap.context.condition.ConditionalOnBean;
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.EmbeddedServletContainer;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
@ -81,7 +82,7 @@ public class EndpointWebMvcChildContextConfiguration implements
@Configuration
@ConditionalOnClass({ EnableWebSecurity.class, Filter.class })
@ConditionalOnBean(name = "springSecurityFilterChain", parentOnly = true)
@ConditionalOnBean(name = "springSecurityFilterChain", search = SearchStrategy.PARENTS)
public static class EndpointWebMvcChildContextSecurityConfiguration {
// FIXME reuse of security filter here is not good. What if totally different

View File

@ -24,6 +24,7 @@ import org.eclipse.jetty.util.Loader;
import org.springframework.autoconfigure.EnableAutoConfiguration;
import org.springframework.bootstrap.context.condition.ConditionalOnClass;
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.EmbeddedServletContainerCustomizerBeanPostProcessor;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
@ -50,7 +51,7 @@ public class EmbeddedServletContainerAutoConfiguration {
* {@link EmbeddedServletContainerCustomizer}s.
*/
@Bean
@ConditionalOnMissingBean(value = EmbeddedServletContainerCustomizerBeanPostProcessor.class, parentContext = false)
@ConditionalOnMissingBean(value = EmbeddedServletContainerCustomizerBeanPostProcessor.class, search = SearchStrategy.CURRENT)
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() {
return new EmbeddedServletContainerCustomizerBeanPostProcessor();
}
@ -64,7 +65,7 @@ public class EmbeddedServletContainerAutoConfiguration {
@Bean
@ConditionalOnMissingBean(value = { ServletContextInitializer.class,
Servlet.class }, parentContext = false)
Servlet.class }, search = SearchStrategy.CURRENT)
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@ -75,7 +76,7 @@ public class EmbeddedServletContainerAutoConfiguration {
*/
@Configuration
@ConditionalOnClass({ Servlet.class, Tomcat.class })
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, parentContext = false)
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedTomcat {
@Bean
@ -90,7 +91,7 @@ public class EmbeddedServletContainerAutoConfiguration {
*/
@Configuration
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class })
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, parentContext = false)
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedJetty {
@Bean

View File

@ -98,13 +98,12 @@ abstract class AbstractOnBeanCondition implements ConfigurationCondition {
String checking = ConditionLogUtils.getPrefix(this.logger, metadata);
Boolean considerHierarchy = (Boolean) metadata.getAnnotationAttributes(
annotationClass().getName()).get("parentContext");
considerHierarchy = (considerHierarchy == null ? false : considerHierarchy);
SearchStrategy search = (SearchStrategy) metadata.getAnnotationAttributes(
annotationClass().getName()).get("search");
search = search == null ? SearchStrategy.ALL : search;
Boolean parentOnly = (Boolean) metadata.getAnnotationAttributes(
annotationClass().getName()).get("parentOnly");
parentOnly = (parentOnly == null ? false : parentOnly);
boolean considerHierarchy = search == SearchStrategy.ALL;
boolean parentOnly = search == SearchStrategy.PARENTS;
List<String> beanClassesFound = new ArrayList<String>();
List<String> beanNamesFound = new ArrayList<String>();

View File

@ -53,13 +53,9 @@ public @interface ConditionalOnBean {
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;
/**
* If only the application parent contexts should be considered.
*/
boolean parentOnly() default false;
SearchStrategy search() default SearchStrategy.ALL;
}

View File

@ -53,8 +53,9 @@ public @interface ConditionalOnMissingBean {
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;
}

View File

@ -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;
}

View File

@ -111,7 +111,7 @@ public class OnMissingBeanConditionTests {
}
@Configuration
@ConditionalOnMissingBean(name = "foo", parentContext = false)
@ConditionalOnMissingBean(name = "foo", search = SearchStrategy.CURRENT)
protected static class HierarchyNotConsidered {
@Bean
public String bar() {