`
TimerBin
  • 浏览: 355555 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

获取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>  

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Util通用工具类(轮子类)

    避免重复造轮子,开发中常用封装...渲染工具类,资源文件相关的操作类,对比两个对象的变化的工具类,Spring的ApplicationContext的持有者,可以用静态方法的方式获取spring容器中的bean,sql语句工具类,高频方法集合类

    第24次课-1 Spring与Hibernate的整合

    通常,程序中采用实现HibernateCallback的匿名内部类来获取HibernateCallback的实例,方法doInHibernate()就是Spring执行的持久化操作。 24.3 Spring对Hibernate的简化 24.3.5 HibernateDaoSupport Spring为与...

    Spring中文帮助文档

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    Spring API

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    程序间耦合.docx

    获取spring的Ioc核心容器,并根据id获取对象 * * ApplicationContext的三个常用实现类: * ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。...

    springmybatis

    其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如IUserOperation.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细...

    java微信公众号MVC开发框架

    spring配置文件applicationContext.xml里面需要配置WeixinConfigurer,这是jwx唯一必须配置项,如果没有配置,启动阶段会报错。 &lt;value&gt;com.telecomjs.yc.controller&lt;/value&gt; component-scan配置了...

    千方百计笔试题大全

    43、说出一些常用的类,包,接口,请各举5 个。 12 44、Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类?是否可以implements(实现)interface(接口)? 12 45、内部类可以引用他包含类的成员吗?有...

    java面试宝典

    43、说出一些常用的类,包,接口,请各举5 个。 12 44、Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类?是否可以implements(实现)interface(接口)? 12 45、内部类可以引用他包含类的成员吗?有...

    J2EE应用开发详解

    91 6.2 CSS 92 6.3 XSLT 94 6.4 小结 96 第7章 Ajax简介 97 7.1 Ajax简介 97 7.2 Ajax技术核心 100 7.2.1 XMLHttpRequest对象的常用方法 100 7.2.2 标准的XMLHttpRequest属性 101 7.3 一个简单的Ajax实例 101 7.4 小...

    乐优商城.xmind

    GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源 BUG 分类不能打开,当添加后却能打开。 修改一天的BUG 最后发现是实体类里属性大小写的问题引起。 注意 Bule_bird 就必须写成 ...

    JAVA核心知识点整理(有效)

    2.2.3. 本地方法区(线程私有) ................................................................................................................ 23 2.2.4. 堆(Heap-线程共享)-运行时数据区 .....................

Global site tag (gtag.js) - Google Analytics