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 no beans meeting the specified requirements
031 * are already contained in the {@link BeanFactory}. None of the requirements must be met
032 * for the condition to match and the requirements 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;ConditionalOnMissingBean
042 *     &#064;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 types of beans that should be checked. The condition matches when no bean
068         * of each class specified is contained in the {@link BeanFactory}.
069         * @return the class types of beans to check
070         */
071        Class<?>[] value() default {};
072
073        /**
074         * The class type names of beans that should be checked. The condition matches when no
075         * bean of each class specified is contained in the {@link BeanFactory}.
076         * @return the class type names of beans to check
077         */
078        String[] type() default {};
079
080        /**
081         * The class types 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 BeanFactory}.
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 BeanFactory}.
106         * @return the names 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        /**
118         * Additional classes that may contain the specified bean types within their generic
119         * parameters. For example, an annotation declaring {@code value=Name.class} and
120         * {@code parameterizedContainer=NameRegistration.class} would detect both
121         * {@code Name} and {@code NameRegistration<Name>}.
122         * @return the container types
123         * @since 2.1.0
124         */
125        Class<?>[] parameterizedContainer() default {};
126
127}