Spring學習(二):Spring xml檔案格式、載入上下文六種方式及作用域
Bean的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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" <!--為名稱空間指定schema檔案 --> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/context/spring-aop-3.0.xsd"> <!--預設名稱空間的配置 --> <bean id="exerciseService" class="com.hellowin.b2c.user.service.ExerciseServiceImpl"/> <!--自定義名稱空間的配置 --> <!-- 使用Annotation自動註冊Bean,解決事物失效問題:在主容器中不掃描@Controller註解,在SpringMvc中只掃描@Controller註解。 --> <context:component-scan base-package="com"/> <!-- provider's application name, used for tracing dependency relationship --> <dubbo:application name="spa-provider"/> <!-- use multicast registry center to export service --> <!--<dubbo:registry address="zookeeper://127.0.0.1:2181"/>--> <dubbo:registry protocol="zookeeper" address="${zookeeper.host}" /> <!-- use dubbo protocol to export service on port 20880 --> <dubbo:protocol name="dubbo" port="20860"/> </beans>
我們在上面的程式碼中定義了三個名稱空間:
- 首先我們定義了一個預設名稱空間,他沒有空間名,用於Spring Bean的定義。
- 接下來我們命名了一個xsi名稱空間,這個名稱空間用於為每個文件中名稱空間指定相對應的schema的樣式檔案。是標準組織定義的標準名稱空間。
- 我們還命名了一個aop的名稱空間,這個名稱空間是Spring配置aop的名稱空間,是使用者自定義的名稱空間。
名稱空間的定義分為了兩個步驟:
- 指定名稱空間的名稱,需要指定名稱空間的縮類名和全名
- 指定名稱空間的schema文件樣式檔案的位置,用空格或回車行來進行分割。
指定名稱空間schema地址有兩個用途:
- xml解析器可以獲取schema檔案,並對文件進行格式合法性驗證
- 在開發環境下,IDE可以用schema檔案來對文件編輯器進行誘導功能。
Spring3.0 的配置Schema檔案分佈在各模組類包中,如果模組擁有對應的Schema檔案,則可以在模組類包中找到一個config目錄,Schema檔案就位於該目錄中,如下是對這些Schema檔案的用途進行了簡單說明:
示例說明:Spring-beans-3.0.xsd
名稱空間:http://www.springframework.org/schema/beans
Schema 檔案:http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
可以看出在Spring3.0當中,所有的Schema檔案的名稱空間以及對應的位置都和Beans這個Schema檔案是類似的。
那麼接下來來了解以下Spring當中其他Schema檔案的用途:
- spring-beans-3.0.xsd:Spring3.0最主要的配置檔案,主要是用於配置Bean
- spring-aop-3.0.xsd:aop配置定義的
- schema spring-tx-3.0.xsd:宣告式事物配置定義的Schema
- spring-mvc-3.0.xsd:Spring3.0當中新增的
- spring-util-3.0.xsd:是為簡化某些複雜的標準配置而提供的Schema
- spring-jee-3.0.xsd:是為簡化J2EE中EJB等功能的配置而提供的Schema
- spring-jdbc-3.0.xsd:為Spring內接資料庫而提供的Schema,3.0新增
- spring-jms-3.0.xsd:jms配置的Schema
- spring-lang-3.0.xsd:增加了對動態語言的支援,為整合動態語言而定義
- spring-oxm-3.0.xsd:配置物件xml對映到schema,3.0新增
- spring-task-3.0.xsd:任務排程的Schema
- spring-tool-3.0.xsd:為整合Schema一些有用工具而提供的Schema
BeanDefinition繼承了AttributeAccessor,說明它具有處理屬性的能力;
BeanDefinition繼承了BeanMetadataElement,說明它可以持有Bean元資料元素,作用是可以持有XML檔案的一個bean標籤對應的Object。
Spring載入上下文
1:spring-mvc.xml中用import引入其他的配置檔案
<import resource="user_spring.xml" />
2:在web.xml配置,應用服務去載入
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml,/WEB-INF/user_spring*.xml</param-value>
</context-param>
3:引用資源用XmlBeanFactory(不能實現多個檔案相互引用)
Resource resource = new ClassPathResource("appcontext.xml");
XmlBeanFactory在3.1以後已經被廢棄,不再推薦使用 可以使用父類DefaultListableBeanFactory
BeanFactory factory = new XmlBeanFactory(resource);
4:引用應用上下文用ClassPathXmlApplicationContext
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("conf/userConfig.xml");
ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml");
5:用檔案系統的路徑引用應用上下文用FileSystemXmlApplicationContext
ApplicationContext factory=new FileSystemXmlApplicationContext("src/applicationContext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml");
6:Web工程定製的載入方法 XmlWebApplicationContext
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext );
SpringBean作用域
在Spring 容器當中,一共提供了5種作用域型別,在配置檔案中,通過屬性scope來設定bean的作用域範圍。
1. singleton:
<bean id="userInfo" class="cn.lovepi.UserInfo" scope="singleton"></bean>
當Bean的作用域為singleton的時候,Spring容器中只會存在一個共享的Bean例項,所有對Bean的請求只要id與bean的定義相匹配,則只會返回bean的同一例項。單一例項會被儲存在單例快取中,為Spring的預設作用域。
2. prototype:
<bean id="userInfo" class="cn.lovepi.UserInfo" scope=" prototype "></bean>
每次對該Bean請求的時候,Spring IoC都會建立一個新的作用域。
對於有狀態的Bean應該使用prototype,對於無狀態的Bean則使用singleton
3. request:
<bean id="userInfo" class="cn.lovepi.UserInfo" scope=" request "></bean>
Request作用域針對的是每次的Http請求,Spring容器會根據相關的Bean的
定義來建立一個全新的Bean例項。而且該Bean只在當前request內是有效的。
4. session:
<bean id="userInfo" class="cn.lovepi.UserInfo" scope=" session "></bean>
針對http session起作用,Spring容器會根據該Bean的定義來建立一個全新的Bean的例項。而且該Bean只在當前http session內是有效的。
5. global session:
<bean id="userInfo" class="cn.lovepi.UserInfo"scope=“globalSession"></bean>
類似標準的http session作用域,不過僅僅在基於portlet的web應用當中才有意義。Portlet規範定義了全域性的Session的概念。他被所有構成某個portlet外部應用中的各種不同的portlet所共享。在global session作用域中所定義的bean被限定於全域性的portlet session的生命週期範圍之內。