一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 淺析Spring配置文件

淺析Spring配置文件

2020-07-31 15:370nise Java教程

本文主要對Spring配置文件進行了介紹。具有很好的參考價值,下面跟著小編一起來看下吧

spring的配置文件概述

簡介

spring的配置文件是用于指導spring工廠進行bean生成、依賴關系注入及bean示例分發的”圖紙”,他是一個或多個標磚的xml文檔,j2ee程序員必須學會靈活應用這份”圖紙”,準確的表達自己的”生成意圖”。

spring配置文件的示例

spring配置文件的一般結構

淺析Spring配置文件

spring容器高層視圖

spring容器啟動基本條件:

spring的框架類包

bean的配置信息

bean的元數據信息

bean的實現類

淺析Spring配置文件

bean的屬性信息

例如:數據源的用戶名、密碼

bean的依賴關系

spring根據依賴關系配置完成bean之間的裝配

bean的行為配置

例如:生命周期范圍、生命周期各個過程的回調函數

bean的創建方式

說明bean是通過構造器還是工廠方法來創建的

bean的實現類

基于xml的配置

spring的配置文件是基于xml格式的,spring1.0的配置采用dtd格式,spring2.0以后使用schema的格式,后者讓不同類型的配置擁有了自己的命名空間,是配置文件更具有擴展性。

xml分析

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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:aop="http://www.springframework.org/schema/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">
<bean id="saleproduct" class="com.sale.entity.saleproduct" ></bean>
<aop:config>
<aop:pointcut expression="execution(* com.sale.service.*.*(..))" id="mycut"/>
</aop:config>
</beans>

xmlns="":表示默認命空間,用于spring bean定

xmlns:xsi="http://www.w3.org/2001/xmlschema-instance":表示xsi標準命名空間,用于指定自定義命名空間的schema文件

xmlns:aop="":表示自定義命名空間,aop表示該命名空間的簡稱

xsi:schemalocation="

 

">:用于為每個命名空間指定具體的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

命名空間:

schema文件:

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>

指定別名,唯一

?
1
2
<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>

必須存在無參數的構造

有參構造

?
1
2
3
<bean id="saleproduct" class="com.sale.entity.saleproduct" >
<constructor-arg name="prodname" value="哈哈" ></constructor-arg>
</bean>

必須存有參數的構造

使用靜態工廠實例化bean

必須的class屬性,factory-method屬性指定實例化bean的方法,而且使用靜態工廠方法也允許指定方法參數,spring ioc容器將調用此屬性指定的方法來獲取bean

?
1
2
3
<bean factory-method="newinstance" id="saleproduct" class="com.sale.entity.saleproduct" >
<constructor-arg index="0" value="hello" ></constructor-arg>
</bean>

使用實例工廠方法實例化bean

不能指定class屬性,factory-bean來指定工廠bean,factory-method指定實例化bean的方法,而且使用實例工廠方法也允許指定參數

?
1
2
3
4
5
6
<!-- 定義實例工廠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層)

淺析Spring配置文件

淺析Spring配置文件

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

prototype

每次從容器中調用bean時,都會返回一個全新的實例,即每次調用getbea()時,相當于執行new bean()的操作。在默認情況下,spring容器在啟動時不實例化propotype的bean。

淺析Spring配置文件

淺析Spring配置文件

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

當用于使用spring的webapplicationconext時,還可以使用另外三種bean的作用域,即request,session和globlesession。在使用web應用環境相關的bean作用域時,必須在web容器中進行一些額外的配置

低版本web容器配置:

?
1
2
3
4
5
6
7
8
<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容器配置:

?
1
2
3
4
5
<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:

?
1
2
3
scope customscope = new threadscope();
beanfactory.registerscope(“thread”,customscope);
<bean id=”***” class=”***” scope=”scopename”>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!

原文鏈接:http://www.cnblogs.com/0nise/p/6323588.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 波多野结衣教师未删减版 | 成人亚洲欧美日韩中文字幕 | 亚洲精品资源 | 人禽l交免费视频观看+视频 | 亚洲高清无码在线 视频 | 拔插拔插成人 | 日本中文字幕不卡在线一区二区 | 国产另类视频 | free性丰满hd性欧美厨房 | 男女真实无遮挡xx00动态图软件 | 四虎影库紧急大通知 | 国产精品视频1区 | 日韩成人一区ftp在线播放 | 骚虎tv| 色偷偷伊人 | 美女扒开腿让男人桶爽动态图片 | tube62hdxxxx日本| 精品高潮呻吟99AV无码视频 | 免费看日产一区二区三区 | ai换脸杨颖被啪在线观看 | 美女被视频网站看免费入口 | 插入影院 | 毛片免费网站 | 亚洲高清免费在线观看 | 美女脱了内裤打开腿让男人图片 | 手机看片国产免费现在观看 | 操熟美女又肥又嫩的骚屁股 | 亚洲精品福利你懂 | 免费高清在线 | 日本精品一区二区三区 | 日本人妖在线 | 国产播放啪视频免费视频 | 俺去啦最新地址 | 精东影业传媒全部作品 | 日本偷拍xxxxxxww | 华人在线视频 | 四虎1515hhcom| 火影小南被爆羞羞网站 | 91人成网站色www | 国产精品嫩草影院一二三区入口 | 视频一本大道香蕉久在线播放 |