欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取spring ApplicationContext常用方法

程序员文章站 2022-04-23 16:53:20
...

背景说明:在开发过程中难免会用到ApplicationContext,在这里记录下笔记。

 

第一种方式:根据配置文件获取长用在工具测试类

 

 

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("spring.xml");
applicationContext.getBean(TimerBin.class);
applicationContext.getBean("timerBin");

 在junit中配置applicationContext  获取bean

 

 

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml" })
public class TimerBinTest {

	@Autowired
	TimerBin timerBin;

	@Test
	public void init() {

	}
}

 

 

第二种方式:通过ServletContext来获取applicationContext 在serverlet 中比较常用

 

 

import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
ServletContext serverContext = null;
//当没获得时会报错,内部还是调用的getWebApplicationContext方法
ApplicationContext applicationContext=WebApplicationContextUtils.getRequiredWebApplicationContext(serverContext);
//当没获得时不会报错
ApplicationContext applicationContext=WebApplicationContextUtils.getWebApplicationContext(serverContext)
applicationContext.getBean(TimerBin.class);
applicationContext.getBean("timerBin");

 当在spring MVC的Controller方法中还可以通过以下两种方式获取配置信息

 

1、

 

import javax.servlet.http.HttpServletRequest;
HttpServletRequest request
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
applicationContext.getBean(TimerBin.class);
applicationContext.getBean("timerBin");

 2、

 

 

import org.springframework.web.context.WebApplicationContext;
import javax.servlet.http.HttpServletRequest;
HttpServletRequest request
WebApplicationContext webApplicationContext = (WebApplicationContext) request.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
webApplicationContext.getBean(TimerBin.class);
webApplicationContext.getBean("timerBin");

 

 

第三种方式:继承ApplicationObjectSupport(抽象类)获取

import org.springframework.context.support.ApplicationObjectSupport;
public class ApplicationContextUtil  extends ApplicationObjectSupport{
	public Object getBean(String name){
		return this.getApplicationContext().getBean(name);
	}
	public <T> T  getBean(Class<T> className){
		return this.getApplicationContext().getBean(className);
	}
}

该类是在没有用SpringMVC时使用,不能获取到ServletContext。 

 

第四种方法:继承WebApplicationObjectSupport(抽象类)获取

import org.springframework.web.context.support.WebApplicationObjectSupport;
public class ApplicationContextUtil2  extends WebApplicationObjectSupport{
public class ApplicationContextUtil2  extends WebApplicationObjectSupport{
	public Object getBean(String name){
		return this.getApplicationContext().getBean(name);
	}
	public <T> T  getBean(Class<T> className){
		return this.getApplicationContext().getBean(className);
	}
	public WebApplicationContext getWebApplicationContexts(){
		return this.getWebApplicationContext();
	}
}

 可以获得WebApplicationContext信息其中包含了ServletContext配置

 

第五种方法:实现ApplicationContextAware接口(最常用,最推荐方式)

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextUtil implements ApplicationContextAware {

	private static ApplicationContext context;
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		this.context=context;
	}
 
	public static ApplicationContext getApplicationContext(){
		return context;
	}
	public Object getBean(String name){
		return context.getBean(name);
	}
	public <T> T  getBean(Class<T> className){
		return context.getBean(className);
	}
}

但是有一点需要在spring的配置文件中对ApplicationContextUtil进行定义

<bean class="cn.timerbin.util.ApplicationContextUtil"></bean>