java-几种上下文以及获取Spring的ApplicationContext的几种方法(整理)

  • Post author:
  • Post category:java

起因是睡觉的时候,我在想如果被面试问道:“你知道怎么可以获取上下文吗?”这个问题我感到很模糊,之前也写过获取上下文,但是记得好像有好几种方法,觉得有点混淆了,所以就想自己好好整理下。

网上搜集的context上下文的几种解释:

Context上下文主要用来从上文传播对象到下文中,他是可以跨线程的。 就是说  class A中你把一个OBJ对象存放到了上下文容器中, 然后你以后的所有线程或你以下调用的所有类中都可以从上下文容器中取出 上面再class A中存放的OBJ对象。

二:

上下文即ServletContext,是一个全局的储存信息的空间,服务器启动,其就存在,服务器关闭,其才释放。
所有用户共用一个ServletContext。所以,为了节省空间,提高效率,ServletContext中,要放必须的、重要的、所有用户需要共享的线程又是安全的一些信息。如,做一个购物类的网站,要从数据库中提取物品信息,如果用session保存这些物品信息,每个用户都访问一便数据库,效率就太低了;所以要用来Servlet上下文来保存,在服务器开始时,就访问数据库,将物品信息存入Servlet上下文中,这样,每个用户只用从上下文中读入物品信息就行了。

三:

A naming service associates names with objects. An association between a name and an object is called a binding, and a set of such bindings is called a context. A name in a context can be bound to another context that uses the same naming conventions; the bound context is called a subcontext. For example, in a filesystem, a directory (such as /temp) is a context that contains bindings between filenames and objects that the system can use to manipulate the files (often called file handles). If a directory contains a binding for another directory (e.g., /temp/javax), the subdirectory is a subcontext. 
(http://blog.csdn.net/centurymagus/article/details/2025455)

四:java中context上下文
http://blog.csdn.net/xiaokui008/article/details/8454949
…….

我的理解就归结为”容器+环境”.

获取上下文:

ServletContext 、ActionContext以及ServletActionContext上下文的介绍



Spring上下文(ApplicationContext)

方法一:ClassPathXmlApplicationContext –从classpath路径加载配置文件,创建Bean对象
ApplicationContext ctx = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
ClassName clazz =(ClassName)ctx.getBean(“beanName”);

方法二:FileSystemXmlApplicationContext  –从指定的目录中加载
ApplicationContext ctx = new FileSystemXmlApplication
Context(“src/applicationContext.xml”);

ClassName clazz =(ClassName)ctx.getBean(“beanName”);


方法三:Spring提供的工具类WebApplicationContextUtils获取ApplicationContext对象(通过ServletContext对象获得ApplicationContext对象,然后根据它获得需要的类实例)
HttpSession session =request.getSession();

ServletContext context = session.getServletContext(); 
//arg0.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
ClassName clazz =(ClassName)ctx.getBean(“beanName”);


上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。




方法四:继承自抽象类ApplicationObjectSupport

说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。

Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。



例如:
import org.springframework.context.support.ApplicationObjectSupport;

public class ContextOne extends ApplicationObjectSupport
{
    ……
}
……..
ContextOne one = new ContextOne();
  one.getApplicationContext();






方法五:继承自抽象类WebApplicationObjectSupport

说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext



例如:
import org.springframework.web.context.support.WebApplicationObjectSupport;
public class ContextOne extends WebApplicationObjectSupport {
    …….
}

……..
ContextOne one = new ContextOne();
  one.getApplicationContext();





方法六:实现接口ApplicationContextAware

当一个类实现了ApplicationContextAware接口后,这个类就可以获得Spring配置文件中的所引用到的bean对象。
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。

Spring初始化时,会通过该方法将ApplicationContext对象注入。



例如:

package com.auth.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
 * 类:Context.java
 * 作者: LYX
 * 时间:2015-11-3
 * 说明:通过接口ApplicationContextAware获得spring上下文
 */
public class Context implements ApplicationContextAware {
 private static ApplicationContext ctx;
//设置ApplicationContext对象
 public void setApplicationContext(ApplicationContext context)
   throws BeansException {
  // TODO Auto-generated method stub
   ctx=context;
 }
 //通过beanName获得实例
 public static Object getBean(String beanName)
 {
  return ctx.getBean(beanName);
 }
}

—————————–

版权声明:本文为u013147600原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。