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 * already contained in the {@link BeanFactory}. When placed on a {@code @Bean} method, 033 * the bean class defaults to the return type of the factory method: 034 * 035 * <pre class="code"> 036 * @Configuration 037 * public class MyAutoConfiguration { 038 * 039 * @ConditionalOnBean 040 * @Bean 041 * public MyService myService() { 042 * ... 043 * } 044 * 045 * }</pre> 046 * <p> 047 * In the sample above the condition will match if a bean of type {@code MyService} is 048 * already contained in the {@link BeanFactory}. 049 * <p> 050 * The condition can only match the bean definitions that have been processed by the 051 * application context so far and, as such, it is strongly recommended to use this 052 * condition on auto-configuration classes only. If a candidate bean may be created by 053 * another auto-configuration, make sure that the one using this condition runs after. 054 * 055 * @author Phillip Webb 056 */ 057@Target({ ElementType.TYPE, ElementType.METHOD }) 058@Retention(RetentionPolicy.RUNTIME) 059@Documented 060@Conditional(OnBeanCondition.class) 061public @interface ConditionalOnBean { 062 063 /** 064 * The class type of bean that should be checked. The condition matches when any of 065 * the classes specified is contained in the {@link ApplicationContext}. 066 * @return the class types of beans to check 067 */ 068 Class<?>[] value() default {}; 069 070 /** 071 * The class type names of bean that should be checked. The condition matches when any 072 * of the classes specified is contained in the {@link ApplicationContext}. 073 * @return the class type names of beans to check 074 */ 075 String[] type() default {}; 076 077 /** 078 * The annotation type decorating a bean that should be checked. The condition matches 079 * when any of the annotations specified is defined on a bean in the 080 * {@link ApplicationContext}. 081 * @return the class-level annotation types to check 082 */ 083 Class<? extends Annotation>[] annotation() default {}; 084 085 /** 086 * The names of beans to check. The condition matches when any of the bean names 087 * specified is contained in the {@link ApplicationContext}. 088 * @return the name of beans to check 089 */ 090 String[] name() default {}; 091 092 /** 093 * Strategy to decide if the application context hierarchy (parent contexts) should be 094 * considered. 095 * @return the search strategy 096 */ 097 SearchStrategy search() default SearchStrategy.ALL; 098 099}