Spring中多配置文件以及尋覓引用其他bean的方式


       Spring多配置文件有什么好處?       

       按照目的、功能去拆分配置文件,可以提高配置文件的可讀性與維護性,如將配置事務管理、數據源等少改動的配置與配置bean單獨分開。

  

 

      Spring讀取配置文件的幾種方式:

      

 1、使用Spring自身提供的ApplicationContext方式讀取

 

      在Java程序中可以使用ApplicationContext兩個實現類ClassPathXmlApplicationContext以及FileSystemXmlApplicationContext來讀取多個配置文件,他們的構造器都可以接收一個配置文件數組。

      如:  ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);與采用FileSystemXmlApplicationContext創建ApplicationContext的方式相似,區別僅在於二者搜索配置文件的路徑不同:ClassPathXmlApplicationContext通過CLASSPATH路徑搜索配置文件:而FileSystemXmlApplicationContext則在當前路徑搜索配置文件。

 

方法一:在初始化時保存ApplicationContext對象 
代碼: 

 

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); 
ac.getBean("beanId"); 

說明: 

這種方式適用於采用Spring框架的獨立應用程序,需要程序通過配置文件手工初始化Spring的情況。 

 

方法二:通過Spring提供的工具類獲取ApplicationContext對象 
代碼: 

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"); 

說明: 

這種方式適合於采用Spring框架的B/S系統,通過ServletContext對象獲取ApplicationContext對象,然后在通過它獲取需要的類實例。 
上面兩個工具方式的區別是,前者在獲取失敗時拋出異常,后者返回null。

 

方法三:繼承自抽象類ApplicationObjectSupport 
說明: 
抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContext。Spring初始化時,會通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。 

方法四:繼承自抽象類WebApplicationObjectSupport 
說明: 
類似上面方法,調用getWebApplicationContext()獲取WebApplicationContext 

方法五:實現接口ApplicationContextAware 
說明: 
實現該接口的setApplicationContext(ApplicationContext context)方法,並保存ApplicationContext 對象。Spring初始化時,會通過該方法將ApplicationContext 對象注入。 

以上方法適合不同的情況,請根據具體情況選用相應的方法。 

 

參考:程序員的墳墓

 

 

  2、使用web工程啟動時加載

 

    在web.xml中配置web容器啟動是自動加載哪些配置文件:

 

 <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>

 

多個的時候可以用 * 號來代替。

<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配置文件中導入其他配置文件

 

     在/WEB-INF/applicationContext.xml配置應用服務去加載,可以在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>  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM