SpringContextHolder 靜態持有SpringContext的引用

SpringContextHolder 靜態持有SpringContext的引用 

package com.test.quartz;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 *
 * 以靜態變量保存Spring ApplicationContext, 可在任何代碼任何地方任何時候中取出ApplicaitonContext.
 * @author zhuh
 * 
 */

public class SpringContextHolder implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

	// 實現ApplicationContextAware接口的context注入函數, 將其存入靜態變量.
	public void setApplicationContext(ApplicationContext applicationContext) {
		SpringContextHolder.applicationContext = applicationContext;

	}

	// 取得存儲在靜態變量中的ApplicationContext.
	public static ApplicationContext getApplicationContext() {
		checkApplicationContext();
		return applicationContext;

	}

	// 從靜態變量ApplicationContext中取得Bean, 自動轉型爲所賦值對象的類型.
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		checkApplicationContext();
		return (T) applicationContext.getBean(name);

	}

	// 從靜態變量ApplicationContext中取得Bean, 自動轉型爲所賦值對象的類型.
	// 如果有多個Bean符合Class, 取出第一個.
	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<T> clazz) {
		checkApplicationContext();
		return (T) applicationContext.getBeansOfType(clazz);
	
	}

	private static void checkApplicationContext() {
		if (applicationContext == null) {
			throw new IllegalStateException("applicaitonContext未注入,請在applicationContext.xml中定義SpringContextHolder");
		}
	}

}

<!-- 用於持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的靜態方法得到spring bean對象 --> 

<bean class="com.xxxxx.SpringContextHolder"  />

該工具類主要用於:那些沒有歸入spring框架管理的類卻要調用spring容器中的bean提供的工具類。

在spring中要通過IOC依賴注入來取得對應的對象,但是該類通過實現ApplicationContextAware接口,以靜態變量保存Spring ApplicationContext, 可在任何代碼任何地方任何時候中取出ApplicaitonContext.

如此就不能說說org.springframework.context.ApplicationContextAware這個接口了:

當一個類實現了這個接口(ApplicationContextAware)之後,這個類就可以方便獲得ApplicationContext中的所有bean。換句話說,就是這個類可以直接獲取spring配置文件中,所有有引用到的bean對象。

除了以上SpringContextHolder類之外,還有不需要多次加載spring配置文件就可以取得bean的類:
 

1.Struts2框架中,在監聽器中有這麼一句

ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());

之後可以用

scheduleService = (IScheduleService)context.getBean("scheduleService");

取到對象,請問context都可以取到什麼信息,這些信息的來源在哪?是XML裏配置了呢,還是固定的一部分信息呢?
2、這個 application封裝的是web.xml 內部的信息
而你的web.xml裏面有spring的配置文件,所有,裏面還包含spring的信息
同樣包含struts2的filter信息
總之就是和web.xml有關係的所有信息

3、在web.xml裏有這麼一段

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>

 那麼在取信息的時候,也會把applicationContext.xml裏的信息取出來

使用方式

/**
 * Hello world!
 */
public class App {
	public static void main(String[] args) {
		
		System.out.println("加載spring");
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:spring/spring-context.xml"});
		context.start();
		
		SpringContextHolder springContextHolder = new SpringContextHolder();
		springContextHolder.setApplicationContext(context);
		//SpringContextHolder.getBean("queryDealService");
		System.out.println("Hello World!");
	}
}

關注公衆號,分享乾貨,討論技術

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