spring的@ConditionalOnMissingBean和@ConditionalOnBean兩個注解的使用

  • 顧名思義兩個注解作用是為了有條件的注入啟用一些配置文件,動態(tài)生成一些bean,也可以理解為springBean A 要依賴于springBean的存在而存在.
  • 兩個注解是相反的,所以只需要解釋一個,第二個也就明白了.

ConditionalOnMissingBean

1.源碼介紹
/*
 * Copyright 2012-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.condition;

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.Conditional;

/**
 * {@link Conditional} that only matches when no beans of the specified classes and/or
 * with the specified names are already contained in the {@link BeanFactory}.
 * <p>
 * When placed on a {@code @Bean} method, the bean class defaults to the return type of
 * the factory method:
 *
 * <pre class="code">
 * &#064;Configuration
 * public class MyAutoConfiguration {
 *
 *     &#064;ConditionalOnMissingBean
 *     &#064;Bean
 *     public MyService myService() {
 *         ...
 *     }
 *
 * }</pre>
 * <p>
 * In the sample above the condition will match if no bean of type {@code MyService} is
 * already contained in the {@link BeanFactory}.
 * <p>
 * The condition can only match the bean definitions that have been processed by the
 * application context so far and, as such, it is strongly recommended to use this
 * condition on auto-configuration classes only. If a candidate bean may be created by
 * another auto-configuration, make sure that the one using this condition runs after.
 *
 * @author Phillip Webb
 * @author Andy Wilkinson
 */
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnMissingBean {

    /**
     * The class types of beans that should be checked. The condition matches when no bean
     * of each class specified is contained in the {@link BeanFactory}.
     * @return the class types of beans to check
     */
    Class<?>[] value() default {};

    /**
     * The class type names of beans that should be checked. The condition matches when no
     * bean of each class specified is contained in the {@link BeanFactory}.
     * @return the class type names of beans to check
     */
    String[] type() default {};

    /**
     * The class types of beans that should be ignored when identifying matching beans.
     * @return the class types of beans to ignore
     * @since 1.2.5
     */
    Class<?>[] ignored() default {};

    /**
     * The class type names of beans that should be ignored when identifying matching
     * beans.
     * @return the class type names of beans to ignore
     * @since 1.2.5
     */
    String[] ignoredType() default {};

    /**
     * The annotation type decorating a bean that should be checked. The condition matches
     * when each annotation specified is missing from all beans in the
     * {@link BeanFactory}.
     * @return the class-level annotation types to check
     */
    Class<? extends Annotation>[] annotation() default {};

    /**
     * The names of beans to check. The condition matches when each bean name specified is
     * missing in the {@link BeanFactory}.
     * @return the names of beans to check
     */
    String[] name() default {};

    /**
     * Strategy to decide if the application context hierarchy (parent contexts) should be
     * considered.
     * @return the search strategy
     */
    SearchStrategy search() default SearchStrategy.ALL;

    /**
     * Additional classes that may contain the specified bean types within their generic
     * parameters. For example, an annotation declaring {@code value=Name.class} and
     * {@code parameterizedContainer=NameRegistration.class} would detect both
     * {@code Name} and {@code NameRegistration<Name>}.
     * @return the container types
     * @since 2.1.0
     */
    Class<?>[] parameterizedContainer() default {};

}

2.個人理解

僅在該注解規(guī)定的類不存在于 spring容器中時,使用該注解的config或者bean聲明才會被實例化到容器中

3.屬性詳解
  • Class<?>[] value : 需要作為條件的類的Class對象數(shù)組
  • String[] type : 需要作為條件的類的Name,Class.getName()
  • Class<? extends Annotation>[] annotation : (用指定注解修飾的bean)條件所需的注解類
  • String[] name : spring容器中bean的名字
  • SearchStrategy search:搜索容器層級,當前容器,父容器
  • Class<?>[] parameterizedContainer :可能在其泛型參數(shù)中包含指定bean類型的其他類
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末配猫,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件灶似,死亡現(xiàn)場離奇詭異,居然都是意外死亡瑞你,警方通過查閱死者的電腦和手機酪惭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來者甲,“玉大人春感,你說我怎么就攤上這事÷哺祝” “怎么了鲫懒?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長刽辙。 經(jīng)常有香客問我窥岩,道長,這世上最難降的妖魔是什么宰缤? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任颂翼,我火速辦了婚禮,結果婚禮上慨灭,老公的妹妹穿的比我還像新娘朦乏。我一直安慰自己,他們只是感情好氧骤,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布呻疹。 她就那樣靜靜地躺著,像睡著了一般筹陵。 火紅的嫁衣襯著肌膚如雪刽锤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天惶翻,我揣著相機與錄音姑蓝,去河邊找鬼。 笑死吕粗,一個胖子當著我的面吹牛纺荧,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼宙暇,長吁一口氣:“原來是場噩夢啊……” “哼输枯!你這毒婦竟也來了?” 一聲冷哼從身側響起占贫,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤桃熄,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后型奥,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瞳收,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年厢汹,在試婚紗的時候發(fā)現(xiàn)自己被綠了螟深。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡烫葬,死狀恐怖界弧,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情搭综,我是刑警寧澤垢箕,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站兑巾,受9級特大地震影響条获,放射性物質發(fā)生泄漏。R本人自食惡果不足惜闪朱,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一月匣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧奋姿,春花似錦、人聲如沸素标。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽头遭。三九已至寓免,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間计维,已是汗流浹背袜香。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留鲫惶,地道東北人蜈首。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親欢策。 傳聞我的和親對象是個殘疾皇子吆寨,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內容