1. 程式人生 > >【深入JAVA EE】Spring配置文件解析

【深入JAVA EE】Spring配置文件解析

article per posted 別名 utf-8 div back port clu

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

郵箱:[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>

1XML 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中能夠包括例如以下屬性:

技術分享

2、Context命名空間

1)、Context標簽:

技術分享

技術分享

<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配置文件解析