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