激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 淺析Spring配置文件

淺析Spring配置文件

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

本文主要對(duì)Spring配置文件進(jìn)行了介紹。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧

spring的配置文件概述

簡(jiǎn)介

spring的配置文件是用于指導(dǎo)spring工廠進(jìn)行bean生成、依賴關(guān)系注入及bean示例分發(fā)的”圖紙”,他是一個(gè)或多個(gè)標(biāo)磚的xml文檔,j2ee程序員必須學(xué)會(huì)靈活應(yīng)用這份”圖紙”,準(zhǔn)確的表達(dá)自己的”生成意圖”。

spring配置文件的示例

spring配置文件的一般結(jié)構(gòu)

淺析Spring配置文件

spring容器高層視圖

spring容器啟動(dòng)基本條件:

spring的框架類包

bean的配置信息

bean的元數(shù)據(jù)信息

bean的實(shí)現(xiàn)類

淺析Spring配置文件

bean的屬性信息

例如:數(shù)據(jù)源的用戶名、密碼

bean的依賴關(guān)系

spring根據(jù)依賴關(guān)系配置完成bean之間的裝配

bean的行為配置

例如:生命周期范圍、生命周期各個(gè)過(guò)程的回調(diào)函數(shù)

bean的創(chuàng)建方式

說(shuō)明bean是通過(guò)構(gòu)造器還是工廠方法來(lái)創(chuàng)建的

bean的實(shí)現(xiàn)類

基于xml的配置

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

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="":表示默認(rèn)命空間,用于spring bean定

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

xmlns:aop="":表示自定義命名空間,aop表示該命名空間的簡(jiǎn)稱

xsi:schemalocation="

 

">:用于為每個(gè)命名空間指定具體的schema文件

<bean id="saleproduct" class="com.sale.entity.saleproduct" ></bean>:為默認(rèn)命名空間中的配置

<aop:config>

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

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

schema文件的用途

spring3.0的配置schema文件分部在各模塊類包中,如果模塊擁有對(duì)應(yīng)的schema文件,則可以在模塊類包中找到一個(gè)config目錄,schema文件就為與該目錄中,如下是對(duì)這些schema文件的用途:

示例說(shuō)明:spring-aop-3.0.xsd

命名空間:

schema文件:

1. spring-beans.xsd :用于配置bean

2. spring-aop-3.0.xsd :aop配置

3. spring-tx-3.0.xsd:聲明式事務(wù)配置的schema

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

5. spring-utils-3.0.xsd:簡(jiǎn)化某些復(fù)雜的標(biāo)準(zhǔn)配置

6. spring-jee-3.0.xsd:是為簡(jiǎn)化jee中ejb和jndi等功能的配置

7. spring-jdbc-3.0.xsd:是3.0新增的,配置spring內(nèi)接數(shù)據(jù)庫(kù)提供的schema

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

9. spring-lang-3.0.xsd:添加了對(duì)動(dòng)態(tài)語(yǔ)言的支持,集成動(dòng)態(tài)語(yǔ)言定義的

10. spring-oxm-3.0.xsd:配置對(duì)象xml映射到schema

11. spring-task-3.0.xsd:任務(wù)調(diào)度的schema

12. spring-tool-3.0.xsd:集成的spring有用工具而定義的schema

spring bean的命名

每個(gè)bean可以有一個(gè)或多個(gè)id,我們把第一個(gè)id成為”標(biāo)識(shí)符”,其余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>

指定多個(gè)name,唯一

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

指定多個(gè)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命名規(guī)范

2、由字母,數(shù)字,下劃線組成

3、駝峰式,第一個(gè)單詞首字母小寫(xiě),從第二個(gè)但是開(kāi)始第首字母大寫(xiě)

spring bean的實(shí)例化

spring ioc容器是如何實(shí)例化bean呢?傳統(tǒng)應(yīng)用程序可以通過(guò) new和反射方式進(jìn)行實(shí)例化bean。二spring ioc容器則需要根據(jù)bean定義的配置元數(shù)據(jù)使用反射機(jī)制來(lái)創(chuàng)建bean。

spring ioc容器創(chuàng)建bean示例的方式

使用構(gòu)造器實(shí)例化bean

默認(rèn)構(gòu)造

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

必須存在無(wú)參數(shù)的構(gòu)造

有參構(gòu)造

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

必須存有參數(shù)的構(gòu)造

使用靜態(tài)工廠實(shí)例化bean

必須的class屬性,factory-method屬性指定實(shí)例化bean的方法,而且使用靜態(tài)工廠方法也允許指定方法參數(shù),spring ioc容器將調(diào)用此屬性指定的方法來(lái)獲取bean

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

使用實(shí)例工廠方法實(shí)例化bean

不能指定class屬性,factory-bean來(lái)指定工廠bean,factory-method指定實(shí)例化bean的方法,而且使用實(shí)例工廠方法也允許指定參數(shù)

?
1
2
3
4
5
6
<!-- 定義實(shí)例工廠bean -->
<bean id="saleproduct1" class="com.sale.entity.saleproduct" ></bean>
<!-- 使用實(shí)例工廠bean -->
<bean id="saleproduct2" factory-bean="saleproduct1" >
<constructor-arg index="0" value="hello" ></constructor-arg>
</bean>

spring bean的作用域

spring bean中所說(shuō)的作用域,在配置文件中即是”scope”。早面向?qū)ο蟪绦蛟O(shè)計(jì)中一般指對(duì)象或變量之間的可見(jiàn)范圍。而在spring容器中是指其創(chuàng)建的bean對(duì)象相對(duì)于其他bean對(duì)象的請(qǐng)求范圍。

spring bean的作用域類型

singleton

spring ioc容器中僅存在一個(gè)bean的實(shí)例,bean以單利方式存在,單實(shí)例模式是最重要的設(shè)置模式之一,在spring中對(duì)此實(shí)現(xiàn)了超越,可以對(duì)那些非線程安全的對(duì)象采用單例模式(一般使用在dao層)

淺析Spring配置文件

淺析Spring配置文件

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

prototype

每次從容器中調(diào)用bean時(shí),都會(huì)返回一個(gè)全新的實(shí)例,即每次調(diào)用getbea()時(shí),相當(dāng)于執(zhí)行new bean()的操作。在默認(rèn)情況下,spring容器在啟動(dòng)時(shí)不實(shí)例化propotype的bean。

淺析Spring配置文件

淺析Spring配置文件

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

當(dāng)用于使用spring的webapplicationconext時(shí),還可以使用另外三種bean的作用域,即request,session和globlesession。在使用web應(yīng)用環(huán)境相關(guān)的bean作用域時(shí),必須在web容器中進(jìn)行一些額外的配置

低版本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

發(fā)起一次http請(qǐng)求的時(shí)候spring會(huì)創(chuàng)建一個(gè)全新實(shí)例

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

session

當(dāng)前會(huì)話

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

global session

httpsession會(huì)話

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

自定義作用域

在spring 2.0中,spring的bean作用域機(jī)制是可以擴(kuò)展的,這意味著,不僅可以使用spring提供的預(yù)定義bean作用域,還可以定義自己的作用域,甚至重啟定義現(xiàn)有的作用域(不提倡這么做,而且不能覆蓋內(nèi)置的sinleton和prototype作用域)

實(shí)現(xiàn)自定義scope類:

org.springframework.bean.factory.config.scope

注冊(cè)自定義scope類:

configurablebeanfactory.registerscope(string scopename,scope scope)

使用自定義的scope:

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

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持服務(wù)器之家!

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品自在线拍 | 亚洲卡通动漫在线观看 | 999久久久久久 | 99精品在线视频观看 | 国产精品99久久免费观看 | 看av网址 | 国产精品成人久久久久a级 av电影在线免费 | 中文字幕精品亚洲 | 91麻豆精品国产91久久久无需广告 | 国产1区2区在线 | 亚洲第一成网站 | 免费视频精品一区二区 | av之家在线观看 | 爱射av| 本站只有精品 | 国产午夜亚洲精品理论片大丰影院 | 国产伦精品一区二区三区在线 | 久久久久久久久久久国产精品 | 久久福利电影网 | 奇米影视亚洲春色 | 欧美三级欧美成人高清www | 免费的毛片| 91精品国产91热久久久做人人 | 毛片免费在线观看视频 | 欧美福利视频一区二区 | 福利免费在线 | 99精品视频网站 | 小雪奶水翁胀公吸小说最新章节 | 亚洲精品av在线 | 91精品国产日韩91久久久久久360 | 一级做人爱c黑人影片 | 久久精片| 精品国产91久久久久久 | 日韩在线观看视频一区二区三区 | 国产精品久久久久久久av三级 | 超碰97最新 | 久久久久久久不卡 | 国产伦精品一区二区三区在线 | 国产免费观看一区二区三区 | 深夜免费视频 | 国产在线精品区 |