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.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.annotation.Conditional;
028
029/**
030 * {@link Conditional} that only matches when beans meeting all the specified requirements
031 * are already contained in the {@link BeanFactory}. All the requirements must be met for
032 * the condition to match, but they do not have to be met by the same bean.
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 * &#064;Configuration
039 * public class MyAutoConfiguration {
040 *
041 *     &#064;ConditionalOnBean
042 *     &#064;Bean
043 *     public MyService myService() {
044 *         ...
045 *     }
046 *
047 * }</pre>
048 * <p>
049 * In the sample above the condition will match if a 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 */
059@Target({ ElementType.TYPE, ElementType.METHOD })
060@Retention(RetentionPolicy.RUNTIME)
061@Documented
062@Conditional(OnBeanCondition.class)
063public @interface ConditionalOnBean {
064
065        /**
066         * The class types of beans that should be checked. The condition matches when beans
067         * of all classes specified are contained in the {@link BeanFactory}.
068         * @return the class types of beans to check
069         */
070        Class<?>[] value() default {};
071
072        /**
073         * The class type names of beans that should be checked. The condition matches when
074         * beans of all classes specified are contained in the {@link BeanFactory}.
075         * @return the class type names of beans to check
076         */
077        String[] type() default {};
078
079        /**
080         * The annotation type decorating a bean that should be checked. The condition matches
081         * when all of the annotations specified are defined on beans in the
082         * {@link BeanFactory}.
083         * @return the class-level annotation types to check
084         */
085        Class<? extends Annotation>[] annotation() default {};
086
087        /**
088         * The names of beans to check. The condition matches when all of the bean names
089         * specified are contained in the {@link BeanFactory}.
090         * @return the names of beans to check
091         */
092        String[] name() default {};
093
094        /**
095         * Strategy to decide if the application context hierarchy (parent contexts) should be
096         * considered.
097         * @return the search strategy
098         */
099        SearchStrategy search() default SearchStrategy.ALL;
100
101        /**
102         * Additional classes that may contain the specified bean types within their generic
103         * parameters. For example, an annotation declaring {@code value=Name.class} and
104         * {@code parameterizedContainer=NameRegistration.class} would detect both
105         * {@code Name} and {@code NameRegistration<Name>}.
106         * @return the container types
107         * @since 2.1.0
108         */
109        Class<?>[] parameterizedContainer() default {};
110
111}