淺析Spring配置文件
Spring的配置文件概述
簡介
Spring的配置文件是用于指導Spring工廠進行Bean生成、依賴關(guān)系注入及Bean示例分發(fā)的”圖紙”,他是一個或多個標磚的XML文檔,J2EE程序員必須學會靈活應用這份”圖紙”,準確的表達自己的”生成意圖”。
Spring配置文件的示例
Spring配置文件的一般結(jié)構(gòu)

Spring容器高層視圖
Spring容器啟動基本條件:
Spring的框架類包
Bean的配置信息
Bean的元數(shù)據(jù)信息
Bean的實現(xiàn)類

Bean的屬性信息
例如:數(shù)據(jù)源的用戶名、密碼
Bean的依賴關(guān)系
Spring根據(jù)依賴關(guān)系配置完成Bean之間的裝配
Bean的行為配置
例如:生命周期范圍、生命周期各個過程的回調(diào)函數(shù)
Bean的創(chuàng)建方式
說明Bean是通過構(gòu)造器還是工廠方法來創(chuàng)建的
Bean的實現(xiàn)類
基于XML的配置
Spring的配置文件是基于XML格式的,Spring1.0的配置采用DTD格式,Spring2.0以后使用Schema的格式,后者讓不同類型的配置擁有了自己的命名空間,是配置文件更具有擴展性。
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>:為默認命名空間中的配置 <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內(nèi)接數(shù)據(jù)庫提供的Schema 8. Spring-jms-3.0.xsd:jms的配置 9. Spring-lang-3.0.xsd:添加了對動態(tài)語言的支持,集成動態(tài)語言定義的 10. Spring-oxm-3.0.xsd:配置對象xml映射到Schema 11. Spring-task-3.0.xsd:任務調(diào)度的Schema 12. Spring-tool-3.0.xsd:集成的Spring有用工具而定義的Schema Spring Bean的命名 每個Bean可以有一個或多個id,我們把第一個id成為”標識符”,其余id叫做id別名,這些id在IoC容器中必須唯一。 Bean id的命名方式 配置全限定類名,唯一 指定id,唯一 指定name,唯一 指定id和name,唯一 指定多個name,唯一 指定多個id,唯一 指定別名,唯一 Bean id的命名約定 1、遵循xml命名規(guī)范 2、由字母,數(shù)字,下劃線組成 3、駝峰式,第一個單詞首字母小寫,從第二個但是開始第首字母大寫 Spring Bean的實例化 Spring IoC容器是如何實例化Bean呢?傳統(tǒng)應用程序可以通過 new和反射方式進行實例化Bean。二Spring IoC容器則需要根據(jù)Bean定義的配置元數(shù)據(jù)使用反射機制來創(chuàng)建Bean。 Spring IoC容器創(chuàng)建Bean示例的方式 使用構(gòu)造器實例化Bean 默認構(gòu)造 必須存在無參數(shù)的構(gòu)造 有參構(gòu)造 必須存有參數(shù)的構(gòu)造 使用靜態(tài)工廠實例化Bean 必須的class屬性,factory-method屬性指定實例化Bean的方法,而且使用靜態(tài)工廠方法也允許指定方法參數(shù),Spring IoC容器將調(diào)用此屬性指定的方法來獲取Bean 使用實例工廠方法實例化Bean 不能指定class屬性,factory-bean來指定工廠Bean,factory-method指定實例化Bean的方法,而且使用實例工廠方法也允許指定參數(shù) Spring Bean的作用域 Spring Bean中所說的作用域,在配置文件中即是”scope”。早面向?qū)ο蟪绦蛟O計中一般指對象或變量之間的可見范圍。而在Spring容器中是指其創(chuàng)建的Bean對象相對于其他Bean對象的請求范圍。 Spring Bean的作用域類型 Singleton Spring IoC容器中僅存在一個Bean的實例,Bean以單利方式存在,單實例模式是最重要的設置模式之一,在Spring中對此實現(xiàn)了超越,可以對那些非線程安全的對象采用單例模式(一般使用在DAO層) prototype 每次從容器中調(diào)用Bean時,都會返回一個全新的實例,即每次調(diào)用getBea()時,相當于執(zhí)行new Bean()的操作。在默認情況下,Spring容器在啟動時不實例化propotype的Bean。 當用于使用Spring的WebApplicationConext時,還可以使用另外三種Bean的作用域,即request,session和globleSession。在使用Web應用環(huán)境相關(guān)的Bean作用域時,必須在Web容器中進行一些額外的配置 低版本web容器配置: 高版本的Web容器配置: request 發(fā)起一次http請求的時候Spring會創(chuàng)建一個全新實例 Session 當前會話 Global session httpSession會話 自定義作用域 在spring 2.0中,Spring的Bean作用域機制是可以擴展的,這意味著,不僅可以使用Spring提供的預定義Bean作用域,還可以定義自己的作用域,甚至重啟定義現(xiàn)有的作用域(不提倡這么做,而且不能覆蓋內(nèi)置的sinleton和prototype作用域) 實現(xiàn)自定義Scope類: 注冊自定義Scope類: 使用自定義的Scope: 以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!<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>
<!-- 定義實例工廠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>


<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.scopeConfigurableBeanFactory.registerScope(String scopeName,Scope scope)
Scope customScope = new ThreadScope();
beanFactory.registerScope(“thread”,customScope);
<bean id=”***” class=”***” scope=”scopeName”>
相關(guān)文章
springboot 自定義LocaleResolver實現(xiàn)切換語言
我們在做項目的時候,往往有很多項目需要根據(jù)用戶的需要來切換不同的語言,使用國際化就可以輕松解決。這篇文章主要介紹了springboot 自定義LocaleResolver切換語言,需要的朋友可以參考下2019-10-10
解決springMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無法加載的問題
下面小編就為大家?guī)硪黄鉀QspringMVC 跳轉(zhuǎn)js css圖片等靜態(tài)資源無法加載的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
詳解MyBatis的XML實現(xiàn)方法(附帶注解方式實現(xiàn))
這篇文章主要詳細介紹了MyBatis的XML實現(xiàn)方法(附帶注解方式實現(xiàn)),文中通過代碼示例給大家講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-05-05

