【深入JAVA EE】Spring設定檔解析

來源:互聯網
上載者:User

標籤:xml配置   port   java ee   定義   產生   frame   掃描   作用   code   

在閱讀的過程中有不論什麼問題,歡迎一起交流

郵箱:[email protected]   

QQ:1494713801

 

 

一、Spring頭資訊

Spring設定檔的頭部資訊通常是固定不變的。但每個標籤都有自己的含義。xml命名空間格式例如以下:

<?xml version="1.0" encoding="UTF-8"?>

<beans  xmlns="http://www.springframework.org/schema/beans"

                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xmlns:context="http://www.springframework.org/schema/context"

                xmlns:cache="http://www.springframework.org/schema/cache"
                xmlns:p="http://www.springframework.org/schema/p" 
                xmlns:task="http://www.springframework.org/schema/task"
                xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd
  http://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> 

<!— xml配置內容 -->

</beans>

 

1、XML Schema命名空間作用:
1)、避免命名衝突,像Java中的package一樣

2)、將不同作用的標籤分門別類(像context命名空間針對組件的標籤)

2、代碼解釋:

1)、xmlns="http://www.springframework.org/schema/beans"

聲明xml檔案預設的命名空間,表示未使用其它命名空間的全部標籤的預設命名空間。

2)、xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

聲明XML Schema 執行個體,聲明後就能夠使用 schemaLocation屬性了

3)、xmlns:context="http://www.springframework.org/schema/context"

 

4)、xmlns:cache="http://www.springframework.org/schema/cache"


5)、xmlns:p="http://www.springframework.org/schema/p" 

給XML設定檔"減肥"的還有一個選擇就是使用p名稱空間。當我們採用了p名稱空間。我們就能夠在bean元素中使用屬性(attribute)來描寫敘述bean的property值。


6)、 xmlns:task="http://www.springframework.org/schema/task"

7)、xmlns:aop="http://www.springframework.org/schema/mvc"

聲明首碼為mvc的命名空間,後面的URL用於標示命名空間的地址不會被解析器用於尋找資訊。其惟一的作用是賦予命名空間一個惟一的名稱。

當命名空間被定義在元素的開始標籤中時,全部帶有同樣首碼的子項目都會與同一個命名空間相關聯。

8)、xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

這個從命名能夠看出個大概。指定Schema的位置這個屬性必須結合命名空間使用。這個屬性有兩個值,第一個值表示須要使用的命名空間。第二個值表示供命名空間使用的 XML schema的位置

 

所以我們須要什麼樣的標籤的時候,就引入什麼樣的命名空間和Schema定義就能夠了。

 

二、Spring設定檔結構

beans標籤中能夠包括4個標籤:

  <alias>為一個定義過的bean起一個別名

  <bean>向Spring容器中定義bean元素

  <description>用來描寫敘述Spring context或每一個bean元素。儘管他會被Spring容器所忽略,但<description>元素能夠通過工具產生屬於你的Spring context文檔

  <import>匯入其它Spring context的定義

 

1、bean命名空間:

1)、標籤bean中能夠包括例如以下元素:

  <constructor-arg>向bean的建構函式注入值或引用,即建構函式注入

  <description>同beans中作用同樣。用來描寫敘述Spring context或每一個bean元素,儘管他會被Spring容器所忽略,但<description>元素能夠通過工具產生屬於你的Spring context文檔

  <lookup-method>用法來取代getter注入,指定一個方法。他會在執行被複寫從而返回一個指定的bean,即getter注入

  <meta>同意為你的bean進行meta配置,僅在一些特殊場合下實用

  <property>為bean的特定屬性注入一個值或者引用。這就是我們常說的setter注入

  <replaced-method>用一個新的實現來取代bean的某個方法

2)、標籤bean中能夠包括例如以下屬性:

 

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUxNTc2MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="550" height="480">

 2、Context命名空間

1)、Context標籤:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDUxNTc2MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="531" height="270">

<context:component-scan/>具體解釋

.假設不想在xml檔案裡配置bean。能夠給我們的類加上spring組件註解,僅僅需再配置下spring的掃描器<context:component-scan/>就能夠實現bean的自己主動載入。

然後在程式中增加註解便可自己主動載入bean,@Component是全部受Spring管理組件的通用形式;而@Repository@Service@Controller則是@Component的細化,用來表示更詳細的用例(比如,分別相應了持久化層、服務層和表現層)。

也就是說,你能用@Component來註解你的組件類,但假設用@Repository@Service@Controller來註解它們,你的類或許能更好地被工具處理,或與切面進行關聯。

<context:component-scan>提供兩個子標籤:<context:include-filter>和<context:exclude-filter>各代表引入和排除的過濾。

有了<context:component-scan>,還有一個<context:annotation-config/>標籤根本能夠移除掉。由於已經被包括進去了。

3、AOP命名空間

1)、AOP標籤

4、JEE命名空間

1)、JEE標籤

【深入JAVA EE】Spring設定檔解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.