打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SpringContextUtil ApplicationContextAware static块中 null

通常在方法中使用。在static中,会出现加载先后的顺序,导致SpringContextUtil中的重写方法还没被调用,就先在其他的类中调用了getBean方法,报null。


Spring中,普通bean可以通过实现ApplicationContextAware得到ApplicationContext,需要重写setApplicationContext。通过setApplicationContext将spring的applicationContext得到,那么spring是什么时候执行setApplicationContext方法的呢?

Spring源码中ApplicationContextAwareProcessor.postProcessBeforeInitialization(),对继承自ApplicationContextAware的bean进行处理,调用其setApplicationContext。而ApplicationContextAwareProcessor是在spring容器start的时候生成的。



https://blog.csdn.net/u013147600/article/details/49616427

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

网上搜集的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);
 }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
获取spring的ApplicationContext几种方式【转】
如何手动获取 Spring 容器中的 bean?
springboot应用获取spring上下文的4种方式
外部系统 访问已初始化的spring实例, 目前找到两种方法
普通Java类获取Spring的bean
如何让spring mvc web应用启动时就执行特定处理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服