Spring - 使用ApplicationContextAware得到一个ApplicationContext对象

本文介绍了一种方法,通过使用ApplicationContextHelper类在不修改原有代码的情况下,实现Spring与老代码的整合,便于在老代码中访问Spring的bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/tohmin/article/details/6015289


最近我我开发的一个系统里边有老的代码, 这些老代码没有使用Spring, 我们准备用Spring改写老代码, 但是写到一半, 发现问题很多,然后我们又不想丢掉我们写好的新代码, 所以我们就需要找到一个方法,使我们能够在老的代码里访问Spring的bean. 我们都知道要访问Spring bean 我们就必须得到一个ApplicationContext 或者 BeanFactory 对象, 而ApplicationContext的BeanFactory 的子类, 拥有更强大的功能,ApplicationContext可以在服务器启动的时候自动实例化所有的bean,而 BeanFactory只有在调用getBean()的时候才去实例化那个bean, 这也是我们为什么要得到一个ApplicationContext对象, 事实上Spring2相关的web应用默认使用的是ApplicationContext对象去实例化bean, 换一句话说, 在服务器启动的时候,Spring容器就已经实例化好了一个ApplicationContext对象,所以我们要在老的代码里尝试去获取这个对象。 但是如何才能得到一个ApplicationContext对象呢?方法很多,最常用的办法就是用ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApplicationContext 等对象去加载Spring配置文件,这样做也是可以, 但是在加载Spring配置文件的时候,就会生成一个新的ApplicaitonContext对象而不是Spring容器帮我们生成的哪一个, 这样就产生了冗余, 所以我们在这里不采用这种加载文件的方式,我们使用ApplicationContextAware让Spring容器传递自己生成的ApplicationContext给我们, 然后我们把这个ApplicationContext设置成一个类的静态变量, 这样我们就随时都可以在老的代码里得到Application的对象了。

 

ApplicationContextHelper

  1. import org.springframework.beans.BeansException;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.ApplicationContextAware;  
  4. /** 
  5.  * 
  6.  * @author MinFei 
  7.  */  
  8. public class ApplicationContextHelper implements ApplicationContextAware {  
  9.     private static ApplicationContext appCtx;  
  10.     /** 
  11.      * 此方法可以把ApplicationContext对象inject到当前类中作为一个静态成员变量。 
  12.      * @param applicationContext ApplicationContext 对象. 
  13.      * @throws BeansException 
  14.      */  
  15.     @Override  
  16.     public void setApplicationContext( ApplicationContext applicationContext ) throws BeansException {  
  17.         appCtx = applicationContext;  
  18.     }  
  19.     /** 
  20.      * 这是一个便利的方法,帮助我们快速得到一个BEAN 
  21.      * @param beanName bean的名字 
  22.      * @return 返回一个bean对象 
  23.      */  
  24.     public static Object getBean( String beanName ) {  
  25.         return appCtx.getBean( beanName );  
  26.     }  
  27. }  
 

 

配置 ApplicationContextHelper

[c-sharp] view plaincopy
  1. <bean id="SpringApplicationContext" class="com.company.helper.ApplicationContextHelper"></bean>  

 

使用些列方法去得到一个bean

[c-sharp] view plaincopy
  1. BeanExample beanExample= (BeanExample )ApplicationContextHelper.getBean( "beanExample" );  

这样我们在老代码里取得了一个Spring配置的对象, 然后我们就可以自由自在的在老代码里边享受Spring提供的功能。

 

 

 

希望本博文对你有帮助。


### Spring 工具类获取 ApplicationContext 对象Spring 框架中,`ApplicationContext` 是核心接口之一,提供了访问应用上下文中各种 Bean 的功能。为了方便开发者更灵活地获取 `ApplicationContext` 实例,通常可以借助工具类来实现这一目标。 以下是几种常见的通过工具类获取 `ApplicationContext` 的方式: #### 方法一:继承 `ApplicationObjectSupport` 通过让工具类继承 `org.springframework.context.ApplicationObjectSupport` 抽象类,并将其标记为组件 (`@Component`) 以便被 Spring 容器管理,在容器启动时会自动调用父类的 `setApplicationContext()` 方法完成注入[^1]。之后可通过重载的方法 `getApplicationContext()` 获得当前的应用上下文实例。 ```java import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationObjectSupport; import org.springframework.stereotype.Component; @Component public class ApplicationContextUtil extends ApplicationObjectSupport { /** * 获取SpringApplicationContext对象 */ public static ApplicationContext getApplicationContext() { return applicationContextHolder.get(); } private static final ThreadLocal<ApplicationContext> applicationContextHolder = new ThreadLocal<>(); @Override protected void initApplicationContext() throws BeansException { super.initApplicationContext(); applicationContextHolder.set(getApplicationContext()); } } ``` #### 方法二:自定义静态变量存储 另一种常见的方式是在工具类内部声明一个静态成员变量保存 `ApplicationContext` 参考引用[^2]。当 Spring 初始化完成后,利用事件监听机制或者手动设置该字段值即可供后续使用。 ```java import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class CustomApplicationContext implements ApplicationContextAware { private static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static Object getBean(String beanName){ return context.getBean(beanName); } } ``` #### 方法三:模拟实现 ApplicationContext 接口 对于某些特殊场景下可能需要自行扩展或简化版的功能支持,则可以根据实际需求重新设计并实现自己的 `ApplicationContext` 类型结构[^3]。不过这种方式较少见于生产环境之中,更多适用于学习研究目的。 关于可能出现 NullPointerException 错误的情况分析如下: 如果按照上述任一种方案操作仍遇到空指针异常问题,除了确认 Servlet 部分逻辑无误外,还需仔细核查是否存在 XML 文件路径拼接错误、命名空间未正确定义等问题导致依赖注入失败情形发生[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值