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

本文详细介绍了Java中获取上下文(Context)的各种方法,包括ServletContext的使用、Spring框架中的ApplicationContext获取方式等,并对比了几种常见方法的特点。
起因是睡觉的时候,我在想如果被面试问道:“你知道怎么可以获取上下文吗?”这个问题我感到很模糊,之前也写过获取上下文,但是记得好像有好几种方法,觉得有点混淆了,所以就想自己好好整理下。

网上搜集的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);
 }
}

-----------------------------
Java 中,通常可以通过以下几种方式使用 `ApplicationContext` 获取上下文信息: #### 实现 `ApplicationContextAware` 接口 可以创建一个工具类实现 `ApplicationContextAware` 接口,在 Spring 容器启动时自动注入 `ApplicationContext`。示例代码如下: ```java package com.ricoh.rapp.ezcx.iwbservice.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext appCtx; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { appCtx = applicationContext; } public static ApplicationContext getApplicationContext() { return appCtx; } public static Object getBean(String beanName) { return appCtx.getBean(beanName); } } ``` 之后就可以通过 `SpringContextUtil.getApplicationContext()` 来获取 `ApplicationContext` 实例,进而获取上下文信息[^3]。 #### 从 `WebApplicationContext` 获取 在 Web 应用中,可以从 `ServletContext` 中获取 `WebApplicationContext`。示例代码如下: ```java import javax.servlet.ServletContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; // 获取 ServletContext ServletContext servletContext = request.getServletContext(); // 获取 WebApplicationContext WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); ``` #### 手动加载配置文件创建 `ApplicationContext` 可以手动加载 Spring 配置文件来创建 `ApplicationContext` 实例。示例代码如下: ```java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; // 加载 XML 配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值