引用外部Spring配置文件

  • 背景

Spring的配置文件有兩種,分別是Spring和Spring MVC的配置文件,一般放在classpath下或者WEB-INF下,加載的方式一般在web.xml中聲明,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    ... ...

    <!-- 指定配置文件路徑 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 初始化Spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 初始化Spring MVC -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定配置文件路徑 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    ... ...

</web-app>

若未指定contextConfigLocation參數,ContextLoaderListener會默認使用/WEB-INF/applicationContext.xml文件,DispatcherServlet會默認使用${servlet-name}-servlet.xml

顯然,這種方式的話Spring的配置文件會被打到war包裏,有時會修改的需要,但下次更新又會被覆蓋掉。

  • 解決

跟蹤源碼看一下Spring的初始化,找到指定配置文件的地方:

ContextLoaderListener.java

// Listener的上下文初始化方法,在此處初始化Spring上下文
@Override
public void contextInitialized(ServletContextEvent event) {
    initWebApplicationContext(event.getServletContext());
}

AbstractApplicationContext.java

@Override
public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        ... ...

        // 初始化BeanFactory,找到配置文件,加載Bean信息
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        ... ...
    }
}

XmlWebApplicationContext.java

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
    // 此處獲取contextConfigLocation參數
    String[] configLocations = getConfigLocations();
    if (configLocations != null) {
        for (String configLocation : configLocations) {
            reader.loadBeanDefinitions(configLocation);
        }
    }
}

AbstractBeanDefinitionReader.java

public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
        ... ...

        if (resourceLoader instanceof ResourcePatternResolver) {
            // Resource pattern matching available.
            try {
                // 使用ResourcePatternResolver,將location也就是contextConfigLocation參數,封裝成Spring的Resource
                Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
                int loadCount = loadBeanDefinitions(resources);

                ......              

                return loadCount;
            }
            catch (IOException ex) {
                throw new BeanDefinitionStoreException(
                        "Could not resolve bean definition resource pattern [" + location + "]", ex);
            }
        }

        ... ...     

    }

這裏我們看到,Spring會利用配置的路徑參數加載成一個Resource,供後續解析。通過查閱官方文檔,Spring提供了幾種內建Resource,有UrlResourceClassPathResourceFileSystemResourceInputStreamResourceByteArrayResource,我們一開始給出web.xml示例的配置路徑最後封裝成了ClassPathResource。

要想引用外部文件,其實可以使用UrlResource這種類型,UrlResource包裝了一個java.net.URL,被用來訪問能通過URL訪問到的對象,比如文件、HTTP資源、FTP資源,具體如何使用呢?
很簡單,將contextConfigLocation參數配置成file:${配置文件路徑},如file:D:/IdeaProjects/spring-framework-demo/config/spring-mvc.xml。然後又有一個問題,我們不想在web.xml寫死路徑,而是想在代碼中注入路徑(在Spring初始化前)。

ContextLoaderListener和DispatcherServlet初始化不相同,因此分開處理:

ContextLoaderListener:寫一個CustomContextLoaderListener類繼承ContextLoaderListener,複寫ContextLoaderListener的contextInitialized(ServletContextEvent event)方法,在調用super.contextInitialized(event)之前利用得到的ServletContext寫入contextConfigLocation參數,具體實現如下:

import org.springframework.web.context.ContextLoaderListener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

public class CustomContextLoaderListener extends ContextLoaderListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        initContextConfigLocation(event.getServletContext());
        super.contextInitialized(event);
    }

    private void initContextConfigLocation(ServletContext context) {
        context.setInitParameter(CONFIG_LOCATION_PARAM, "file:D:/IdeaProjects/spring-framework-demo/config/applicationContext.xml");
    }

}

DispatcherServlet:寫一個CustomDispatcherServlet類繼承DispatcherServlet,在無參構造器中,調用setContextConfigLocation()方法,注入contextConfigLocation參數,具體實現如下:

import org.springframework.web.servlet.DispatcherServlet;

public class CustomDispatcherServlet extends DispatcherServlet {
    private static final long serialVersionUID = 4556688394661662171L;

    public CustomDispatcherServlet() {
        super();

        initContextConfigLocation();
    }

    private void initContextConfigLocation() {
        super.setContextConfigLocation("file:D:/IdeaProjects/spring-framework-demo/config/spring-mvc.xml");
    }

}

最後,在web.xml聲明自定義的ContextLoaderListener和DispatcherServlet,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    ... ...

    <listener>
        <listener-class>com.yjy.listener.CustomContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>com.yjy.servlet.CustomDispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    ... ...

</web-app>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章