淺析Spring設定檔,淺析spring

來源:互聯網
上載者:User

淺析Spring設定檔,淺析spring
Spring的設定檔概述簡介

Spring的設定檔是用於指導Spring工廠進行Bean產生、依賴關係注入及Bean樣本分發的”圖紙”,他是一個或多個標磚的XML文檔,J2EE程式員必須學會靈活應用這份”圖紙”,準確的表達自己的”產生意圖”。

Spring設定檔的樣本

Spring設定檔的一般結構

Spring容器高層視圖

Spring容器啟動基本條件:

Spring的架構類包

Bean的配置資訊

Bean的中繼資料資訊

Bean的實作類別

Bean的屬性資訊

例如:資料來源的使用者名稱、密碼

Bean的依賴關係

Spring根據依賴關係配置完成Bean之間的裝配

Bean的行為配置

例如:生命週期範圍、生命週期各個過程的回呼函數

Bean的建立方式

說明Bean是通過構造器還是Factory 方法來建立的

Bean的實作類別

 

基於XML的配置

Spring的設定檔是基於XML格式的,Spring1.0的配置採用DTD格式,Spring2.0以後使用Schema的格式,後者讓不同類型的配置擁有了自己的命名空間,是設定檔更具有擴充性。

XML分析

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3      xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 9 <bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>10 <aop:config>11 <aop:pointcut expression="execution(* com.sale.service.*.*(..))" id="mycut"/>12 </aop:config>13 </beans>

xmlns="http://www.springframework.org/schema/beans":表示預設命空間,用於Spring Bean定

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance":表示xsi標準命名空間,用於指定自訂命名空間的schema檔案

xmlns:aop="http://www.springframework.org/schema/aop":表示自訂命名空間,aop表示該命名空間的簡稱

xsi:schemaLocation="

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

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">:用於為每個命名空間指定具體的schema檔案

<bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>:為預設命名空間中的配置

<aop:config>

<aop:pointcut expression="execution(* com.sale.service.*.*(..))" id="mycut"/>

</aop:config>:為aop命名空間的配置

Schema檔案的用途

Spring3.0的配置Schema檔案分部在各模組類包中,如果模組擁有對應的Schema檔案,則可以在模組類包中找到一個config目錄,Schema檔案就為與該目錄中,如下是對這些Schema檔案的用途:

樣本說明:spring-aop-3.0.xsd

命名空間:http://www.springframework.org/schema/aop

Schema檔案:http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

1. Spring-beans.xsd :用於配置Bean

2. Spring-aop-3.0.xsd :AOP配置

3. Spring-tx-3.0.xsd:聲明式事務配置的Schema

4. Spring-mvc-3.0.xsd:3.0新增的

5. Spring-utils-3.0.xsd:簡化某些複雜的標準配置

6. Spring-jee-3.0.xsd:是為簡化jee中ejb和jndi等功能的配置

7. Spring-jdbc-3.0.xsd:是3.0新增的,配置Spring內接資料庫提供的Schema

8. Spring-jms-3.0.xsd:jms的配置

9. Spring-lang-3.0.xsd:添加了對動態語言的支援,整合動態語言定義的

10. Spring-oxm-3.0.xsd:設定物件xml映射到Schema

11. Spring-task-3.0.xsd:任務調度的Schema

12. Spring-tool-3.0.xsd:整合的Spring有用工具而定義的Schema

Spring Bean的命名

每個Bean可以有一個或多個id,我們把第一個id成為”標識符”,其餘id叫做id別名,這些id在IoC容器中必須唯一。

Bean id的命名方式

配置全限定類名,唯一

<bean class="com.sale.entity.SaleProduct" ></bean>

指定id,唯一

<bean  id="saleProduct"class="com.sale.entity.SaleProduct" ></bean>

指定name,唯一

<bean name="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

指定id和name,唯一

<beanid="saleProduct"name="saleProduct"class="com.sale.entity.SaleProduct" ></bean>

指定多個name,唯一

<bean name="bean1;alias1;alias2" class="com.sale.entity.SaleProduct" ></bean>

指定多個id,唯一

<bean id="bean1;alias1;alias2" class="com.sale.entity.SaleProduct" ></bean>

指定別名,唯一

<bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean><alias name="saleProduct" alias="alias1"/>
Bean id的命名規範

1、遵循xml命名規範

2、由字母,數字,底線組成

3、駝峰式,第一個單詞首字母小寫,從第二個但是開始第首字母大寫

Spring Bean的執行個體化

Spring IoC容器是如何執行個體化Bean呢?傳統應用程式可以通過 new和反射方式進行執行個體化Bean。二Spring IoC容器則需要根據Bean定義的配置中繼資料使用反射機制來建立Bean。

Spring IoC容器建立Bean樣本的方式

使用構造器執行個體化Bean

預設構造

<bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

必須存在無參數的構造

有參構造

<bean id="saleProduct" class="com.sale.entity.SaleProduct" ><constructor-arg name="prodName" value="哈哈" ></constructor-arg></bean>

必須存有參數的構造

使用靜態工廠執行個體化Bean

必須的class屬性,factory-method屬性指定執行個體化Bean的方法,而且使用靜態Factory 方法也允許指定方法參數,Spring IoC容器將調用此屬性指定的方法來擷取Bean

<bean factory-method="newInstance" id="saleProduct" class="com.sale.entity.SaleProduct" ><constructor-arg index="0" value="Hello" ></constructor-arg></bean>

使用執行個體Factory 方法執行個體化Bean

不能指定class屬性,factory-bean來指定工廠Bean,factory-method指定執行個體化Bean的方法,而且使用執行個體Factory 方法也允許指定參數

<!-- 定義執行個體工廠Bean --><bean id="saleProduct1" class="com.sale.entity.SaleProduct"  ></bean><!-- 使用執行個體工廠Bean --><bean id="saleProduct2" factory-bean="saleProduct1" ><constructor-arg index="0" value="Hello" ></constructor-arg></bean>

Spring Bean的範圍

Spring Bean中所說的範圍,在設定檔中即是”scope”。早物件導向程式設計中一般指對象或變數之間的可見範圍。而在Spring容器中是指其建立的Bean對象相對於其他Bean對象的請求範圍。

Spring Bean的範圍類型

Singleton

Spring IoC容器中僅存在一個Bean的執行個體,Bean以單利方式存在,單一實例模式是最重要的設定模式之一,在Spring中對此實現了超越,可以對那些非安全執行緒的對象採用單例模式(一般使用在DAO層)


<bean scope="singleton" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

prototype

每次從容器中調用Bean時,都會返回一個全新的執行個體,即每次調用getBea()時,相當於執行new Bean()的操作。在預設情況下,Spring容器在啟動時不執行個體化propotype的Bean。

 

 

<bean scope="prototype" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

當用於使用Spring的WebApplicationConext時,還可以使用另外三種Bean的範圍,即request,session和globleSession。在使用Web應用環境相關的Bean範圍時,必須在Web容器中進行一些額外的配置

低版本web容器配置:

<filter> <filter-name>requestContextFilter</filter-name> <filter-class>org.springframework.web.RequestConextFilter</filter-class></filter><filter-mapping> <filter-name>requestContextFilter</filter-name> <servlet-name>/*</servlet-name></filter-mapping>

高版本的Web容器配置:

<listener> <listener-class>  org.springframework.web.context.request.RequestConextLinstener </listener-class></listener>

request

發起一次http請求的時候Spring會建立一個全新執行個體

<bean scope="request" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

Session

當前會話

<bean scope="Session" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

Global session

httpSession會話

<bean scope="Global session" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
自訂範圍

在spring 2.0中,Spring的Bean範圍機制是可以擴充的,這意味著,不僅可以使用Spring提供的預定義Bean範圍,還可以定義自己的範圍,甚至重啟定義現有的範圍(不提倡這麼做,而且不能覆蓋內建的sinleton和prototype範圍)

實現自訂Scope類:

Org.springframework.bean.factory.config.scope

註冊自訂Scope類:

ConfigurableBeanFactory.registerScope(String scopeName,Scope scope)

使用自訂的Scope:

 

Scope customScope = new ThreadScope();beanFactory.registerScope(“thread”,customScope);<bean id=”***” class=”***” scope=”scopeName”>

 

原創作品,侵權必究!
Email:woo0nise@gamail.com
交流QQ群:193306983

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.