1. 程式人生 > >詳解Spring框架註解掃描開啟之配置細節

詳解Spring框架註解掃描開啟之配置細節

今天通過做的一個結合ssh的crm專案,最後用到了spring註解配置,最後需要開啟配置,查找了很長時間,找了一篇比較有質量的部落格文章,就轉載過來,希望能夠給與大家一定幫助,另外也希望博主的文章有更多的人看到。

原文地址:https://blog.csdn.net/u014427391/article/details/72722797

原文地址:https://www.jb51.net/article/121113.htm

 

前言

Spring框架對Bean進行裝配提供了很靈活的方式,下面歸納一下主要的方式:

  • 在XML中進行顯示配置
  • 在Java中進行顯示配置
  • 隱式的bean發現機制和自動裝配

而自動裝配實現就需要註解掃描,這時發現了兩種開啟註解掃描的方式,即<context:annotation-config/>和<context:component-scan>

下面歸納一下這兩種方式的異同點:

<context:annotation-config>:註解掃描是針對已經在Spring容器裡註冊過的Bean

<context:component-scan>:不僅具備<context:annotation-config>的所有功能,還可以在指定的package下面掃描對應的bean

Demo:

Demo:XML註冊Bean方式

下面給出兩個類,類A和類B

package com.test;

pubic class B{


  public B(){

    System.out.println("B類");

  }

}
 

 

package com.test;

public class A {

  private B bClass;


  public void setBClass(B bClass){

    this.bClass = bClass;

    System.out.println("通過set的方式注入B類");

  }


  public A(){

    System.out.println("A類");

  }

}
 

 

如何我們這時可以通過傳統的xml配置在Application.xml裡進行bean註冊

<bean id="bBean"class="com.test.B"/>

<bean id="aBean"class="com.test.A">

 <property name="bClass" ref="bBean"/>

</bean>
 

 

啟動載入Application.xml

輸出:
類B
類A
通過set的方式注入B類

Demo:annotation配置註解開啟方式

package com.test;

pubic class B{


  public B(){

    System.out.println("B類");

  }

}
 

 

package com.test;

public class A {

  private B bClass;


  @Autowired

  public void setBClass(B bClass){

    this.bClass = bClass;

    System.out.println("通過set的方式注入B類");

  }


  public A(){

    System.out.println("A類");

  }

}
 

 

然後我們需要在Application.xml裡註冊Bean,假如我們先這樣配置,僅僅註冊Bean,不開啟掃描

<bean id="bBean"class="com.test.B"/>

<bean id="aBean"class="com.test.A"/>
 

 

或者僅僅開啟掃描,不註冊Bean

<context:annotation-config/>

 

 

這時載入Application.xml配置

輸出:
類B
類A

我們會發現下面的@Autowired的方法是不能被載入的

@Autowired

  public void setBClass(B bClass){

    this.bClass = bClass;

    System.out.println("通過set的方式注入B類");

  }
 

 

解決方法:

修改Application.xml配置檔案

<context:annotation-config/>

<bean id="bBean"class="com.test.B"/>

<bean id="aBean"class="com.test.A"/>
 

 

重新載入配置檔案,輸出正常了

輸出:
類B
類A
通過set的方式注入B類

歸納:<context:annotation-config>:註解掃描是針對已經在Spring容器裡註冊過的Bean

Demo:component配置註解開啟方式

package com.test;

pubic class B{


  public B(){

    System.out.println("B類");

  }

}
 

 

package com.test;

@Component

public class A {

  private B bClass;


  @Autowired

  public void setBClass(B bClass){

    this.bClass = bClass;

    System.out.println("通過set的方式注入B類");

  }


  public A(){

    System.out.println("A類");

  }

}
 

 

然後我們配置一下application.xml,開啟annotaion-config掃描

<context:annotation-config />

 

 

載入配置檔案:

輸出:
類B
類A

原因:<context:annotation-config>:註解掃描是針對已經在Spring容器裡註冊過的Bean,Bean並沒有註冊過,所以即使開啟了@Autowired、@Component註解 和配置開啟了annotaion-config掃描還是載入不到

修改配置檔案:

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

 

 

重新載入配置檔案:

輸出:
類B
類A
通過set的方式注入B類

歸納:

<context:annotation-config>:註解掃描是針對已經在Spring容器裡註冊過的Bean

<context:component-scan>:不僅具備<context:annotation-config>的所有功能,還可以在指定的package下面掃描對應的bean

<context:annotation-config />和 <context:component-scan>同時存在的時候,前者會被忽略。

即使註冊Bean,同時開啟<context:annotation-config />掃描,@autowire,@resource等注入註解只會被注入一次,也即只加載一次

 

以上就是本文的全部內容,希望對大家的學習有所幫助