淺析Spring配置文件
Spring的配置文件概述
簡(jiǎn)介
Spring的配置文件是用于指導(dǎo)Spring工廠進(jìn)行Bean生成、依賴(lài)關(guān)系注入及Bean示例分發(fā)的”圖紙”,他是一個(gè)或多個(gè)標(biāo)磚的XML文檔,J2EE程序員必須學(xué)會(huì)靈活應(yīng)用這份”圖紙”,準(zhǔn)確的表達(dá)自己的”生成意圖”。
Spring配置文件的示例
Spring配置文件的一般結(jié)構(gòu)
Spring容器高層視圖
Spring容器啟動(dòng)基本條件:
Spring的框架類(lèi)包
Bean的配置信息
Bean的元數(shù)據(jù)信息
Bean的實(shí)現(xiàn)類(lèi)
Bean的屬性信息
例如:數(shù)據(jù)源的用戶(hù)名、密碼
Bean的依賴(lài)關(guān)系
Spring根據(jù)依賴(lài)關(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)類(lèi)
基于XML的配置
Spring的配置文件是基于XML格式的,Spring1.0的配置采用DTD格式,Spring2.0以后使用Schema的格式,后者讓不同類(lèi)型的配置擁有了自己的命名空間,是配置文件更具有擴(kuò)展性。
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: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="
xmlns:xsi="
xmlns:aop="
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop
<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文件分部在各模塊類(lèi)包中,如果模塊擁有對(duì)應(yīng)的Schema文件,則可以在模塊類(lèi)包中找到一個(gè)config目錄,Schema文件就為與該目錄中,如下是對(duì)這些Schema文件的用途: 示例說(shuō)明: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:聲明式事務(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的命名方式 配置全限定類(lèi)名,唯一 指定id,唯一 指定name,唯一 指定id和name,唯一 指定多個(gè)name,唯一 指定多個(gè)id,唯一 指定別名,唯一 Bean id的命名約定 1、遵循xml命名規(guī)范 2、由字母,數(shù)字,下劃線(xiàn)組成 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)造 必須存在無(wú)參數(shù)的構(gòu)造 有參構(gòu)造 必須存有參數(shù)的構(gòu)造 使用靜態(tài)工廠實(shí)例化Bean 必須的class屬性,factory-method屬性指定實(shí)例化Bean的方法,而且使用靜態(tài)工廠方法也允許指定方法參數(shù),Spring IoC容器將調(diào)用此屬性指定的方法來(lái)獲取Bean 使用實(shí)例工廠方法實(shí)例化Bean 不能指定class屬性,factory-bean來(lái)指定工廠Bean,factory-method指定實(shí)例化Bean的方法,而且使用實(shí)例工廠方法也允許指定參數(shù) 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的作用域類(lèi)型 Singleton Spring IoC容器中僅存在一個(gè)Bean的實(shí)例,Bean以單利方式存在,單實(shí)例模式是最重要的設(shè)置模式之一,在Spring中對(duì)此實(shí)現(xiàn)了超越,可以對(duì)那些非線(xiàn)程安全的對(duì)象采用單例模式(一般使用在DAO層) 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。 當(dāng)用于使用Spring的WebApplicationConext時(shí),還可以使用另外三種Bean的作用域,即request,session和globleSession。在使用Web應(yīng)用環(huán)境相關(guān)的Bean作用域時(shí),必須在Web容器中進(jìn)行一些額外的配置 低版本web容器配置: 高版本的Web容器配置: request 發(fā)起一次http請(qǐng)求的時(shí)候Spring會(huì)創(chuàng)建一個(gè)全新實(shí)例 Session 當(dāng)前會(huì)話(huà) Global session httpSession會(huì)話(huà) 自定義作用域 在spring 2.0中,Spring的Bean作用域機(jī)制是可以擴(kuò)展的,這意味著,不僅可以使用Spring提供的預(yù)定義Bean作用域,還可以定義自己的作用域,甚至重啟定義現(xiàn)有的作用域(不提倡這么做,而且不能覆蓋內(nèi)置的sinleton和prototype作用域) 實(shí)現(xiàn)自定義Scope類(lèi): 注冊(cè)自定義Scope類(lèi): 使用自定義的Scope: 以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!<bean class="com.sale.entity.SaleProduct" ></bean>
<bean id="saleProduct"class="com.sale.entity.SaleProduct" ></bean>
<bean name="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
<beanid="saleProduct"name="saleProduct"class="com.sale.entity.SaleProduct" ></bean>
<bean name="bean1;alias1;alias2" class="com.sale.entity.SaleProduct" ></bean>
<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="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
<bean id="saleProduct" class="com.sale.entity.SaleProduct" >
<constructor-arg name="prodName" value="哈哈" ></constructor-arg>
</bean>
<bean factory-method="newInstance" id="saleProduct" class="com.sale.entity.SaleProduct" >
<constructor-arg index="0" value="Hello" ></constructor-arg>
</bean>
<!-- 定義實(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>
<bean scope="singleton" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
<bean scope="prototype" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
<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>
<listener>
<listener-class>
org.springframework.web.context.request.RequestConextLinstener
</listener-class>
</listener>
<bean scope="request" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
<bean scope="Session" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
<bean scope="Global session" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
Org.springframework.bean.factory.config.scope
ConfigurableBeanFactory.registerScope(String scopeName,Scope scope)
Scope customScope = new ThreadScope();
beanFactory.registerScope(“thread”,customScope);
<bean id=”***” class=”***” scope=”scopeName”>
相關(guān)文章
springboot 自定義LocaleResolver實(shí)現(xiàn)切換語(yǔ)言
我們?cè)谧鲰?xiàng)目的時(shí)候,往往有很多項(xiàng)目需要根據(jù)用戶(hù)的需要來(lái)切換不同的語(yǔ)言,使用國(guó)際化就可以輕松解決。這篇文章主要介紹了springboot 自定義LocaleResolver切換語(yǔ)言,需要的朋友可以參考下2019-10-10解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無(wú)法加載的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無(wú)法加載的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10詳解MyBatis的XML實(shí)現(xiàn)方法(附帶注解方式實(shí)現(xiàn))
這篇文章主要詳細(xì)介紹了MyBatis的XML實(shí)現(xiàn)方法(附帶注解方式實(shí)現(xiàn)),文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-05-05