获取spring ApplicationContext常用方法

背景说明:在开发过程中难免会用到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>  

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值