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()
本文深入探讨了ApplicationContextAware接口的实现与使用方法,包括如何通过接口获取Spring上下文实例,以及在实际项目中遇到的问题解决策略。同时,介绍了在SSH和Struts2框架下获取WebApplicationContext的详细步骤,并讨论了相关异常处理。
4994

被折叠的 条评论
为什么被折叠?



