001/* 002 * Copyright 2012-2016 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.Documented; 020import java.lang.annotation.ElementType; 021import java.lang.annotation.Retention; 022import java.lang.annotation.RetentionPolicy; 023import java.lang.annotation.Target; 024 025import org.springframework.beans.factory.BeanFactory; 026import org.springframework.context.ApplicationContext; 027import org.springframework.context.annotation.Conditional; 028 029/** 030 * {@link Conditional} that only matches when the specified bean class is already 031 * contained in the {@link BeanFactory} and a single candidate can be determined. 032 * <p> 033 * The condition will also match if multiple matching bean instances are already contained 034 * in the {@link BeanFactory} but a primary candidate has been defined; essentially, the 035 * condition match if auto-wiring a bean with the defined type will succeed. 036 * <p> 037 * The condition can only match the bean definitions that have been processed by the 038 * application context so far and, as such, it is strongly recommended to use this 039 * condition on auto-configuration classes only. If a candidate bean may be created by 040 * another auto-configuration, make sure that the one using this condition runs after. 041 * 042 * @author Stephane Nicoll 043 * @since 1.3.0 044 */ 045@Target({ ElementType.TYPE, ElementType.METHOD }) 046@Retention(RetentionPolicy.RUNTIME) 047@Documented 048@Conditional(OnBeanCondition.class) 049public @interface ConditionalOnSingleCandidate { 050 051 /** 052 * The class type of bean that should be checked. The condition match if the class 053 * specified is contained in the {@link ApplicationContext} and a primary candidate 054 * exists in case of multiple instances. 055 * <p> 056 * This attribute may <strong>not</strong> be used in conjunction with {@link #type()} 057 * , but it may be used instead of {@link #type()}. 058 * @return the class type of the bean to check 059 */ 060 Class<?> value() default Object.class; 061 062 /** 063 * The class type name of bean that should be checked. The condition matches if the 064 * class specified is contained in the {@link ApplicationContext} and a primary 065 * candidate exists in case of multiple instances. 066 * <p> 067 * This attribute may <strong>not</strong> be used in conjunction with 068 * {@link #value()}, but it may be used instead of {@link #value()}. 069 * @return the class type name of the bean to check 070 */ 071 String type() default ""; 072 073 /** 074 * Strategy to decide if the application context hierarchy (parent contexts) should be 075 * considered. 076 * @return the search strategy 077 */ 078 SearchStrategy search() default SearchStrategy.ALL; 079 080}