001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.condition; 018 019import java.lang.annotation.Annotation; 020import java.lang.annotation.Documented; 021import java.lang.annotation.ElementType; 022import java.lang.annotation.Retention; 023import java.lang.annotation.RetentionPolicy; 024import java.lang.annotation.Target; 025 026import org.springframework.beans.factory.BeanFactory; 027import org.springframework.context.ApplicationContext; 028import org.springframework.context.annotation.Conditional; 029 030/** 031 * {@link Conditional} that only matches when the specified bean classes and/or names are 032 * not already contained in the {@link BeanFactory}. 033 * <p> 034 * When placed on a {@code @Bean} method, the bean class defaults to the return type of 035 * the factory method: 036 * 037 * <pre class="code"> 038 * @Configuration 039 * public class MyAutoConfiguration { 040 * 041 * @ConditionalOnMissingBean 042 * @Bean 043 * public MyService myService() { 044 * ... 045 * } 046 * 047 * }</pre> 048 * <p> 049 * In the sample above the condition will match if no bean of type {@code MyService} is 050 * already contained in the {@link BeanFactory}. 051 * <p> 052 * The condition can only match the bean definitions that have been processed by the 053 * application context so far and, as such, it is strongly recommended to use this 054 * condition on auto-configuration classes only. If a candidate bean may be created by 055 * another auto-configuration, make sure that the one using this condition runs after. 056 * 057 * @author Phillip Webb 058 * @author Andy Wilkinson 059 */ 060@Target({ ElementType.TYPE, ElementType.METHOD }) 061@Retention(RetentionPolicy.RUNTIME) 062@Documented 063@Conditional(OnBeanCondition.class) 064public @interface ConditionalOnMissingBean { 065 066 /** 067 * The class type of bean that should be checked. The condition matches when each 068 * class specified is missing in the {@link ApplicationContext}. 069 * @return the class types of beans to check 070 */ 071 Class<?>[] value() default {}; 072 073 /** 074 * The class type names of bean that should be checked. The condition matches when 075 * each class specified is missing in the {@link ApplicationContext}. 076 * @return the class type names of beans to check 077 */ 078 String[] type() default {}; 079 080 /** 081 * The class type of beans that should be ignored when identifying matching beans. 082 * @return the class types of beans to ignore 083 * @since 1.2.5 084 */ 085 Class<?>[] ignored() default {}; 086 087 /** 088 * The class type names of beans that should be ignored when identifying matching 089 * beans. 090 * @return the class type names of beans to ignore 091 * @since 1.2.5 092 */ 093 String[] ignoredType() default {}; 094 095 /** 096 * The annotation type decorating a bean that should be checked. The condition matches 097 * when each annotation specified is missing from all beans in the 098 * {@link ApplicationContext}. 099 * @return the class-level annotation types to check 100 */ 101 Class<? extends Annotation>[] annotation() default {}; 102 103 /** 104 * The names of beans to check. The condition matches when each bean name specified is 105 * missing in the {@link ApplicationContext}. 106 * @return the name of beans to check 107 */ 108 String[] name() default {}; 109 110 /** 111 * Strategy to decide if the application context hierarchy (parent contexts) should be 112 * considered. 113 * @return the search strategy 114 */ 115 SearchStrategy search() default SearchStrategy.ALL; 116 117}