自定義加載Spring配置文件

        爲了適應目前框架的插件啓動機制(同一平臺不同項目加載不同插件和配置文件),不得不想辦法讓插件來選擇性的加載spring配置文件,我是通過重寫spring監聽器來實現加載自定義配置文件。

一、首先,重寫類的主要代碼(主要在中文註釋處):

public class ShineContextLoaderListener extends ContextLoaderListener{
	private ContextLoader contextLoader;

	@Override
	protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) {
//		return super.createWebApplicationContext(sc, parent);
		Class<?> contextClass = determineContextClass(sc);
		if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
			throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
					"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
		}
		ConfigurableWebApplicationContext wac =
				(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

		// Assign the best possible id value.
		if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
			// Servlet <= 2.4: resort to name specified in web.xml, if any.
			String servletContextName = sc.getServletContextName();
			wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
					ObjectUtils.getDisplayString(servletContextName));
		}
		else {
			// Servlet 2.5's getContextPath available!
			try {
				String contextPath = (String) ServletContext.class.getMethod("getContextPath").invoke(sc);
				wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
						ObjectUtils.getDisplayString(contextPath));
			}
			catch (Exception ex) {
				throw new IllegalStateException("Failed to invoke Servlet 2.5 getContextPath method", ex);
			}
		}

		//將默認的spring配置文件與插件注入的配置文件融合
		String configLocation = sc.getInitParameter(CONFIG_LOCATION_PARAM);	//這裏獲取到web.xml中通過param配置的spring配置文件路徑
		String fusionConfigLocation = ConfigFactory.getFactory().fusionSpringXml(configLocation, "Spring");	//將動態加載的spring配置文件和默認的配置文件拼接在一起
                /* 這裏拼出結果類似:classpath:com/shine/docs/config/dbSpring.xml,classpath:com/shine/jbpm/config/jbpmSpring.xml */
		
		wac.setParent(parent);
		wac.setServletContext(sc);
		wac.setConfigLocation(fusionConfigLocation);	//設置配置文件路徑爲拼接後的值
		customizeContext(sc, wac);
		wac.refresh();
		return wac;
	}

	/**
	 * Initialize the root web application context.
	 */
	public void contextInitialized(ServletContextEvent event) {
		//初始化系統配置,將啓動時需要的配置文件加載到系統全局變量中
		ConfigFactory.getFactory().init(event.getServletContext());
		
		this.contextLoader = createContextLoader();
		if (this.contextLoader == null) {
			this.contextLoader = this;
		}
		this.contextLoader.initWebApplicationContext(event.getServletContext());
	}
}
二、將web.xml中的配置的spring監聽器改成重寫後的類

<!-- 
	Spring監聽器,原class:org.springframework.web.context.ContextLoaderListener 
	contextConfigLocation值已改成通過插件加載,在此處保留的話不要與插件中加載的衝突
	如果同時使用SpringMvc則注意不要重複加載 
-->
<listener>
	<listener-class>com.shine.spring.ShineContextLoaderListener</listener-class>
</listener>
<!-- 
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>
 -->

OK,主要就是這樣。

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