Spring中多配置文件及引用其他bean的方式
Spring多配置文件有什么好處?
按照目的、功能去拆分配置文件,可以提高配置文件的可讀性與維護(hù)性,如將配置事務(wù)管理、數(shù)據(jù)源等少改動(dòng)的配置與配置bean單獨(dú)分開(kāi)。

Spring讀取配置文件的幾種方式:
1、使用Spring自身提供的ApplicationContext方式讀取
在Java程序中可以使用ApplicationContext兩個(gè)實(shí)現(xiàn)類(lèi)ClassPathXmlApplicationContext以及FileSystemXmlApplicationContext來(lái)讀取多個(gè)配置文件,他們的構(gòu)造器都可以接收一個(gè)配置文件數(shù)組。
如: ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);與采用FileSystemXmlApplicationContext創(chuàng)建ApplicationContext的方式相似,區(qū)別僅在于二者搜索配置文件的路徑不同:ClassPathXmlApplicationContext通過(guò)CLASSPATH路徑搜索配置文件:而FileSystemXmlApplicationContext則在當(dāng)前路徑搜索配置文件。
方法一:在初始化時(shí)保存ApplicationContext對(duì)象
代碼:
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
說(shuō)明:
這種方式適用于采用Spring框架的獨(dú)立應(yīng)用程序,需要程序通過(guò)配置文件手工初始化Spring的情況。
方法二:通過(guò)Spring提供的工具類(lèi)獲取ApplicationContext對(duì)象
代碼:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)
ac1.getBean("beanId");
ac2.getBean("beanId");
說(shuō)明:
這種方式適合于采用Spring框架的B/S系統(tǒng),通過(guò)ServletContext對(duì)象獲取ApplicationContext對(duì)象,然后在通過(guò)它獲取需要的類(lèi)實(shí)例。
上面兩個(gè)工具方式的區(qū)別是,前者在獲取失敗時(shí)拋出異常,后者返回null。
方法三:繼承自抽象類(lèi)ApplicationObjectSupport
說(shuō)明:
抽象類(lèi)ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContext。Spring初始化時(shí),會(huì)通過(guò)該抽象類(lèi)的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對(duì)象注入。
方法四:繼承自抽象類(lèi)WebApplicationObjectSupport
說(shuō)明:
類(lèi)似上面方法,調(diào)用getWebApplicationContext()獲取WebApplicationContext
方法五:實(shí)現(xiàn)接口ApplicationContextAware
說(shuō)明:
實(shí)現(xiàn)該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 對(duì)象。Spring初始化時(shí),會(huì)通過(guò)該方法將ApplicationContext 對(duì)象注入。
以上方法適合不同的情況,請(qǐng)根據(jù)具體情況選用相應(yīng)的方法。
2、使用web工程啟動(dòng)時(shí)加載
在web.xml中配置web容器啟動(dòng)是自動(dòng)加載哪些配置文件:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/spring-core.xml</param-value> </context-param> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
多個(gè)的時(shí)候可以用 * 號(hào)來(lái)代替。
<servlet> <servlet-name>app</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml,/WEB- INF/user_spring.xml</param-value> </context-param> <load-on-startup>1</load-on-startup> </servlet>
3、Xml配置文件中導(dǎo)入其他配置文件
在/WEB-INF/applicationContext.xml配置應(yīng)用服務(wù)去加載,可以在applicationContext.xml中用import引入其他的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <import resource="spring-servlet.xml"/> <import resource="spring-security.xml"/> <import resource="spring-hibernate.xml"/> <import resource="spring-redis.xml"/> </beans>
相關(guān)文章
詳解json在SpringBoot中的格式轉(zhuǎn)換
這篇文章主要介紹了詳解json在SpringBoot中的格式轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java中Retry方法的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Java中Retry方法的簡(jiǎn)單實(shí)現(xiàn),Retry主要是利用Java的lambda表達(dá)式和線程接口實(shí)現(xiàn)有返回值和無(wú)返回值的重試,思考了下就寫(xiě)了一個(gè)簡(jiǎn)易R(shí)etry功能分享出來(lái),需要的朋友可以參考下2024-01-01
詳解Kotlin和anko融合進(jìn)行Android開(kāi)發(fā)
本篇文章主要介紹了Kotlin和anko融合進(jìn)行Android開(kāi)發(fā),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11

