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

spring工程 使用ApplicationContextAware接口获得spring上下文

 
阅读更多

ApplicationContextAware接口

 

 

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

 

@Component

public class My1 implements ApplicationContextAware {

 

public static ApplicationContext act=null;

public void setApplicationContext(ApplicationContext arg0)

throws BeansException {

act=arg0;

}

 

}

 

使用时

My1.act.getBean("object name");

 

其它:

ssh项目中,时常使用WebApplicationContext,来获得spring上下文,如下:

WebApplicationContext wac= null;

wac=WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());

但如果struts2框架未能在调用前加载,则不能正常使用。

// 未成功时,会抛异常

WebApplicationContextUtils.getRequiredWebApplicationContext();

// 为成功时,会返回null

WebApplicationContextUtils.getWebApplicationContext()

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics