Spring多配置文件有什么好處?
按照目的、功能去拆分配置文件,可以提高配置文件的可讀性與維護(hù)性,如將配置事務(wù)管理、數(shù)據(jù)源等少改動(dòng)的配置與配置bean單獨(dú)分開(kāi)。
Spring讀取配置文件的幾種方式:
1、使用Spring自身提供的ApplicationContext方式讀取
在Java程序中可以使用ApplicationContext兩個(gè)實(shí)現(xiàn)類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ì)象
代碼:
1
2
|
ApplicationContext ac = new FileSystemXmlApplicationContext( "applicationContext.xml" ); ac.getBean( "beanId" ); |
說(shuō)明:
這種方式適用于采用Spring框架的獨(dú)立應(yīng)用程序,需要程序通過(guò)配置文件手工初始化Spring的情況。
方法二:通過(guò)Spring提供的工具類獲取ApplicationContext對(duì)象
代碼:
1
2
3
4
5
|
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ò)它獲取需要的類實(shí)例。
上面兩個(gè)工具方式的區(qū)別是,前者在獲取失敗時(shí)拋出異常,后者返回null。
方法三:繼承自抽象類ApplicationObjectSupport
說(shuō)明:
抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContext。Spring初始化時(shí),會(huì)通過(guò)該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對(duì)象注入。
方法四:繼承自抽象類WebApplicationObjectSupport
說(shuō)明:
類似上面方法,調(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)加載哪些配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<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)代替。
1
2
3
4
5
6
7
8
9
10
11
12
|
<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引入其他的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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> |