Spring中ApplicationContextAware的作用
来源:互联网 发布:软件开发工作室名称 编辑:程序博客网 时间:2024/05/30 22:44
在项目中,我们会经常使用注解的方式来加载我们需要的dao或者service,但是有些类我们没有让Spring帮我们自动加载,而我们需要在其中使用dao层中的方法或者service层中的方法,该怎么办呢?
这个时候我们就会想到这个接口(ApplicationContextAware)这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有引用到的bean对象(dao或者service层我们一般都是以包路径整个扫描)。废话我就不多说了,直接上代码
/** * 当一个类实现了ApplicationContextAware接口之后, * 这个类就可以方便获得ApplicationContext中的所有bean。 * 换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。 * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext. */@Service@Lazy(false)public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class); /** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } /** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); } /** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ public static <T> T getBean(Class<T> requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } /** * 清除SpringContextHolder中的ApplicationContext为Null. */ public static void clearHolder() { if (logger.isDebugEnabled()){ logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext); } applicationContext = null; } /** * 实现ApplicationContextAware接口, 注入Context到静态变量中. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; } /** * 实现DisposableBean接口, 在Context关闭时清理静态变量. */ @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); } /** * 检查ApplicationContext不为空. */ private static void assertContextInjected() { Validate.validState(applicationContext != null, "applicaitonContext属性未注入," + " 请在applicationContext.xml中定义SpringContextHolder."); }
在其他地方我们这样获取bean
private static DbMovieService dbMovieService = SpringContextHolder.getBean(DbMovieService.class);private static DbMovieDao dao = SpringContextHolder.getBean(DbMovieDao.class);
是不是很方便,任何地方都可以哟!
阅读全文
1 0
- Spring中ApplicationContextAware的作用
- Spring中ApplicationContextAware接口的作用
- Spring中ApplicationContextAware的理解
- Spring中ApplicationContextAware接口的使用
- Spring中ApplicationContextAware接口的用法
- ApplicationContextAware接口的作用
- ApplicationContextAware接口的作用
- ApplicationContextAware接口的作用
- spring中ApplicationContextAware接口
- Spring中ApplicationContextAware使用说明
- Spring的ApplicationContextAware解析
- Spring的ApplicationContextAware解析
- Spring中ApplicationContextAware接口用法
- Spring 的 ApplicationEvent ,ApplicationListener,ApplicationContextAware
- spring ApplicationContextAware的那些事儿
- spring ApplicationContextAware
- Spring ApplicationContextAware
- Spring ApplicationContextAware
- 科大讯飞2018校园招聘9月16号编程题
- (M)Dynamic Programming:322. Coin Change
- 自定义view之刮刮乐
- [BZOJ]1210: [HNOI2004]邮递员 插头DP+高精度
- hdu 2067 小兔的棋盘
- Spring中ApplicationContextAware的作用
- CCleaner后门事件文章搜集
- go中结构体与json的格式转换
- 我的知识点
- MySQL日期时间函数大全
- Objective-C中的访问权限
- 良心教程:用户交互的黑科技ARKitQQQ1
- zTree:实现单选/取消父子关联等
- Git和gitHub