日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

context:annotation-config 跟 context:component-scan诠释及区别

發布時間:2023/12/9 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 context:annotation-config 跟 context:component-scan诠释及区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

<context:annotation-config>?是用于激活那些已經在spring容器里注冊過的bean(無論是通過xml的方式還是通過package sanning的方式)上面的注解。

<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>還可以在指定的package下掃描以及注冊javabean?。

?

下面我們通過例子來詳細查看他們的區別,

有三個class ? A,B,C,并且B,C的對象被注入到A中.

?

[java] view plaincopyprint?
  • package?com.xxx;??
  • public?class?B?{??
  • ??public?B()?{??
  • ????System.out.println("creating?bean?B:?"?+?this);??
  • ??}??
  • }??
  • ??
  • package?com.xxx;??
  • public?class?C?{??
  • ??public?C()?{??
  • ????System.out.println("creating?bean?C:?"?+?this);??
  • ??}??
  • }??
  • ??
  • package?com.yyy;??
  • import?com.xxx.B;??
  • import?com.xxx.C;??
  • public?class?A?{???
  • ??private?B?bbb;??
  • ??private?C?ccc;??
  • ??public?A()?{??
  • ????System.out.println("creating?bean?A:?"?+?this);??
  • ??}??
  • ??public?void?setBbb(B?bbb)?{??
  • ????System.out.println("setting?A.bbb?with?"?+?bbb);??
  • ????this.bbb?=?bbb;??
  • ??}??
  • ??public?void?setCcc(C?ccc)?{??
  • ????System.out.println("setting?A.ccc?with?"?+?ccc);??
  • ????this.ccc?=?ccc;???
  • ??}??
  • }??
  • ?

    在applicationContext.xml中加入下面的配置 :

    ?

    <bean id="bBean"class="com.xxx.B"/> <bean id="cBean"class="com.xxx.C"/> <bean id="aBean"class="com.yyy.A"><property name="bbb" ref="bBean"/><property name="ccc" ref="cBean"/> </bean>

    ?

    加載applicationContext.xml配置文件,將得到下面的結果:

    ?

    creating bean B: com.xxx.B@c2ff5 creating bean C: com.xxx.C@1e8a1f6 creating bean A: com.yyy.A@1e152c5 setting A.bbb with com.xxx.B@c2ff5 setting A.ccc with com.xxx.C@1e8a1f6

    ?

    OK, 這個結果沒什么好說的,就是完全通過xml的方式,不過太過時了,下面通過注解的方式來簡化我們的xml配置文件

    首先,我們使用autowire的方式將對象bbb和ccc注入到A中:

    ?

    ?

    [java] view plaincopyprint?
  • package?com.yyy;??
  • import?org.springframework.beans.factory.annotation.Autowired;??
  • import?com.xxx.B;??
  • import?com.xxx.C;??
  • public?class?A?{???
  • ??private?B?bbb;??
  • ??private?C?ccc;??
  • ??public?A()?{??
  • ????System.out.println("creating?bean?A:?"?+?this);??
  • ??}??
  • ??@Autowired??
  • ??public?void?setBbb(B?bbb)?{??
  • ????System.out.println("setting?A.bbb?with?"?+?bbb);??
  • ????this.bbb?=?bbb;??
  • ??}??
  • ??@Autowired??
  • ??public?void?setCcc(C?ccc)?{??
  • ????System.out.println("setting?A.ccc?with?"?+?ccc);??
  • ????this.ccc?=?ccc;??
  • ??}??
  • }??
  • ?

    ?

    然后,我們就可以從applicationContext.xml中移除下面的配置

    ?

    <property name="bbb" ref="bBean"/> <property name="ccc" ref="cBean"/>

    ?

    移除之后,我們的applicationContext.xml配置文件就簡化為下面的樣子了

    ?

    <bean id="bBean"class="com.xxx.B"/> <bean id="cBean"class="com.xxx.C"/> <bean id="aBean"class="com.yyy.A"/>

    ?

    當我們加載applicationContext.xml配置文件之后,將得到下面的結果:

    ?

    creating bean B: com.xxx.B@5e5a50 creating bean C: com.xxx.C@54a328 creating bean A: com.yyy.A@a3d4cf

    ?

    OK, 結果是錯誤的的,究竟是因為什么呢?為什么我們的屬性沒有被注入進去呢?

    是因為注解本身并不能夠做任何事情,它們只是最基本的組成部分,我們需要能夠處理這些注解的處理工具來處理這些注解

    這就是<context:annotation-config>?所做的事情

    我們將applicationContext.xml配置文件作如下修改:

    ?

    <context:annotation-config /> <bean id="bBean"class="com.xxx.B"/> <bean id="cBean"class="com.xxx.C"/> <bean id="aBean"class="com.yyy.A"/>

    ?

    當我們加載applicationContext.xml配置文件之后,將得到下面的結果:

    ?

    creating bean B: com.xxx.B@15663a2 creating bean C: com.xxx.C@cd5f8b creating bean A: com.yyy.A@157aa53 setting A.bbb with com.xxx.B@15663a2 setting A.ccc with com.xxx.C@cd5f8b

    ?

    OK, 結果正確了

    但是如果我們將代碼作如下修改:

    ?

    ?

    [java] view plaincopyprint?
  • package?com.xxx;??
  • import?org.springframework.stereotype.Component;??
  • @Component??
  • public?class?B?{??
  • ??public?B()?{??
  • ????System.out.println("creating?bean?B:?"?+?this);??
  • ??}??
  • }??
  • ??
  • package?com.xxx;??
  • import?org.springframework.stereotype.Component;??
  • @Component??
  • public?class?C?{??
  • ??public?C()?{??
  • ????System.out.println("creating?bean?C:?"?+?this);??
  • ??}??
  • }??
  • ??
  • package?com.yyy;??
  • import?org.springframework.beans.factory.annotation.Autowired;??
  • import?org.springframework.stereotype.Component;??
  • import?com.xxx.B;??
  • import?com.xxx.C;??
  • @Component??
  • public?class?A?{???
  • ??private?B?bbb;??
  • ??private?C?ccc;??
  • ??public?A()?{??
  • ????System.out.println("creating?bean?A:?"?+?this);??
  • ??}??
  • ??@Autowired??
  • ??public?void?setBbb(B?bbb)?{??
  • ????System.out.println("setting?A.bbb?with?"?+?bbb);??
  • ????this.bbb?=?bbb;??
  • ??}??
  • ??@Autowired??
  • ??public?void?setCcc(C?ccc)?{??
  • ????System.out.println("setting?A.ccc?with?"?+?ccc);??
  • ????this.ccc?=?ccc;??
  • ??}??
  • }??
  • ?

    applicationContext.xml配置文件修改為:

    ?

    <context:annotation-config />

    ?

    當我們加載applicationContext.xml配置文件之后,卻沒有任何輸出,這是為什么呢?

    那是因為<context:annotation-config />僅能夠在已經在已經注冊過的bean上面起作用。

    對于沒有在spring容器中注冊的bean,它并不能執行任何操作。

    但是不用擔心,<context:component-scan>除了具有<context:annotation-config />的功能之外,還具有自動將帶有@component,@service,@Repository等注解的對象注冊到spring容器中的功能。

    我們將applicationContext.xml配置文件作如下修改:

    ?

    <context:component-scan base-package="com.xxx"/>

    ?

    當我們加載applicationContext.xml的時候,會得到下面的結果:

    ?

    creating bean B: com.xxx.B@1be0f0a creating bean C: com.xxx.C@80d1ff

    ?

    這是什么原因呢?

    是因為我們僅僅掃描了com.xxx包及其子包的類,而class ?A是在com.yyy包下,所以就掃描不到了

    下面我們在applicationContext.xml中把com.yyy也加入進來:

    ?

    <context:component-scan base-package="com.xxx"/><context:component-scan base-package="com.xxx,com.yyy"/> 然后加載applicationContext.xml就會得到下面的結果: creating bean B: com.xxx.B@cd5f8b creating bean C: com.xxx.C@15ac3c9 creating bean A: com.yyy.A@ec4a87 setting A.bbb with com.xxx.B@cd5f8b setting A.ccc with com.xxx.C@15ac3c9

    ?

    哇,結果正確啦 !

    回頭看下我們的applicationContext.xml文件,已經簡化為:

    ?

    <context:component-scan base-package="com.xxx"/><context:component-scan base-package="com.xxx,com.yyy"/>

    ?

    了。

    ?

    那如果我們在applicationContext.xml手動加上下面的配置,也就是說既在applicationContext.xml中手動的注冊了A的實例對象,同時,通過component-scan去掃描并注冊B,C的對象

    ?

    <context:component-scan base-package="com.xxx"/> <bean id="aBean"class="com.yyy.A"/>

    ?

    結果仍是正確的:

    ?

    creating bean B: com.xxx.B@157aa53 creating bean C: com.xxx.C@ec4a87 creating bean A: com.yyy.A@1d64c37 setting A.bbb with com.xxx.B@157aa53 setting A.ccc with com.xxx.C@ec4a87

    ?

    雖然class ?A并不是通過掃描的方式注冊到容器中的 ,但是<context:component-scan>?所產生的的處理那些注解的處理器工具,會處理所有綁定到容器上面的bean,不管是通過xml手動注冊的還是通過scanning掃描注冊的。

    那么,如果我們通過下面的方式呢?我們既配置了<context:annotation-config />,又配置了<context:component-scan base-package="com.xxx" />,它們都具有處理在容器中注冊的bean里面的注解的功能。會不會出現重復注入的情況呢?

    ?

    <context:annotation-config /><context:component-scan base-package="com.xxx"/><bean id="aBean"class="com.yyy.A"/>

    ?

    不用擔心,不會出現的:

    ?

    creating bean B: com.xxx.B@157aa53 creating bean C: com.xxx.C@ec4a87 creating bean A: com.yyy.A@1d64c37 setting A.bbb with com.xxx.B@157aa53 setting A.ccc with com.xxx.C@ec4a87

    ?

    因為<context:annotation-config />和?<context:component-scan>同時存在的時候,前者會被忽略。也就是那些@autowire,@resource等注入注解只會被注入一次

    哪怕是你手動的注冊了多個處理器,spring仍然只會處理一次:

    ?

    [xml] view plaincopyprint?
  • <context:annotation-config?/>??
  • <context:component-scan?base-package="com.xxx"?/>??
  • <bean?id="aBean"?class="com.yyy.A"?/>??
  • <bean?id="bla"?class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"?/>??
  • <bean?id="bla1"?class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"?/>??
  • <bean?id="bla2"?class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"?/>??
  • <bean?id="bla3"?class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"?/>??
  • 結果仍是正確的:

    ?

    creating bean B: com.xxx.B@157aa53 creating bean C: com.xxx.C@ec4a87 creating bean A: com.yyy.A@25d2b2 setting A.bbb with com.xxx.B@157aa53 setting A.ccc with com.xxx.C@ec4a87

    轉載于:https://www.cnblogs.com/sandea/p/3437324.html

    總結

    以上是生活随笔為你收集整理的context:annotation-config 跟 context:component-scan诠释及区别的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。