Spring的配置文件概述
Spring的配置文件是用於指導Spring工廠進行Bean的生產、依賴關係注入及Bean實例分發的"圖紙",它是一個或多個標準的XML文檔,J2EE程序員必須學會並靈活應用這份“圖紙”,準確表達自己的"生產意圖"
Spring配置文件示例
Spring配置文件的一般結構如下:
<beans> <import resource="resource1.xml"/> <import resource="resource2.xml"/> <bean id="bean1" class="**"></bean> <bean name="bean2" class=""></bean> <bean alias="bean3" name="bean2"/> </beans>
Spring容器高層視圖
Spring容器啓動基本條件
Spring的框架類包
Bean的配置信息
xml bean,java configuration, autowire
Bean的實現類
Bean的元數據信息
Bean的實現類
Bean的屬性信息
Bean的依賴關係
Bean的行爲配置
Bean的創建方式
基於XMl的配置
SpringBean的命名
SpringBean的實例化
Spring Ioc容器如何實例化Bean呢?傳統應用程序可以通過new和反射方式進行實例化Bean,而Spring IoC容器則需要根據Bean定義裏的配置元數據使用反射機制來創建Bean.在Spring IoC容器中主要有以下幾種創建Bean實例的方式:
使用構造器實例化Bean
構造器實例化Bean是最簡單的方式,Spring IoC容器既能使用默認空構造器也能使用有構造器兩種方式創建Bean,如下所示:
1、空構造器實例化:
<bean id="helloServiceNoWidthArgs" class="com.hello"/>
2、有參數構造器實例化
<bean id="helloServiceWithArgs" class="com.hello">
<constructor-args index="0" value="hello Spring">
</bean>
使用靜態工程方式實例化Bean
使用靜態工廠的方式除了指定必須的class屬性,還要指定factory-method屬性來指定實例化Bean的方法,而且使用靜態工廠方法也允許指定方法參數,Spring IoC容器將調用此屬指定的方法來獲取Bean,配置如下:
<bean id="helloService" class="com.hello" factory-method="newInstance">
<constructor-args index="0" value="hello factory">
</bean>
使用實例工廠方法實例化Bean
使用實例工廠方式不能指定class屬性,此時必須使用factory-bean屬性來指定工廠Bean,factory-method屬性指定實例化Bean的方法,而且使用實例工廠方法允許指定方法參數,方式和使用構造器方式一樣,
定義實例工廠bean
<bean id="beanInstanceFactory" class="com.helloService" />
<bean id="helloOne" factory-bean="beanInstanceFactory" factory-method="newInstance">
<constructor-arg index="0" value="hello"></constructor-arg>
</bean>
SpringBean的作用域
Spring Bean中所說的作用域,在配置文件中即是"scope"。在面向對象程序設計中一般指對象或變量之間的可見範圍。而在Spring容器中是指其創建的Bean對象相對於其他Bean對象的請求可見範圍:
Bean的作用域類型與配置
在Spring容器當中,一共提供了5種作用域類型,在配置文件中,通過屬性scope來設置Bean的作用域範圍。
singleton: <bean id="userInfo" class="com.UserInfo" scope="singleton"></bean>
prototype:<bean id="userinfo" class="com.Userinfo scope="prototype""></bean>
request:<bean id="userinfo" class="com.Userinfo" scope="request"></bean>
session:<bean id="userInfo" class="com.Userinfo" scope="session"></bean>
global session: <bean id="userinfo" class="com.userinfo" scope="globalSession"></bean>
singleton作用域是指在Spring IoC容器中僅存在一個Bean示例,Bean以單實例的方式存在,單實例模式是最重要的設計模式之一,在Spring中對此實現了超越,可以對那些非線程安全的對象採用單實例模式
singleton配置方式
<bean id="car" class="com.car" scope="singleton"></bean>
<bean id="boss1" class="com.boss" p:car-ref="car"></bean>
<bean id="boss2" class="com.boss" p:car-ref="car"></bean>
<bean id="boss3" class="com.boss" p:car-ref="car"></bean>
prototype作用域是指每次從容器中調用bean時,都返回一個新的實例,即每次調用getBean()時,相當於執行new Bean()的操作。在默認情況下,Spring容器在啓動時不實例化prototype的Bean。
prototype的配置方式
<bean id="car" class="com.car" scope="prototype"></bean>
<bean id="boss1" class="com.boss" p:car-ref="car"></bean>
<bean id="boss2" class="com.boss" p:car-ref="car"></bean>
<bean id="boss3" class="com.boss" p:car-ref="car"></bean>
當用戶使用Spring的webApplicationContext時,還可以使用另外3種Bean的作用域,即request,session和global session.在使用web應用環境相關的bean作用域時,必須在web容器中進行一些額外的配置:
低版本web容器配置
<filter>
<filter-name></filter-name>
<filter-class></filter-class>
</filter>
<filter-mapping>
<filter-name></filter-name>
<servlet-name></servlet-name>
</filter-mapping>
高版本web容器
<listenner>
<listener-class></listenner-class>
</listener>
web應用環境的作用域
request作用域
session作用域
globalsession作用域
配置文件的整合
多個配置文件
spring-common.xml位於 common文件夾下
spring-connection.xml位於connection文件夾下
spring-module.xml位於module文件夾下
傳統加載方式:
ApplicaitonContext context=new ClassPathXmlApplicationContext(new String[]{"Spring-common.xml","Spring-connection.xml","spring-module.xml"})
配置整合文件
spring-all-module.xml
<beans>
<import resource ="common/spring-common.xml"/>
<import resource="connection/spring-connection.xml"/>
<import resource="module/spring-module.xml"/>
</beans>
整合後加載方式:
applicationCongtext context = new ClassPathXmlApplicationContext("spring-all-module.xml")