实现ApplicationContextAware接口时,获取ApplicationContext为null
将懒加载关闭,@Lazy(false),默认为true
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; @Service
@Lazy(false)
public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext = null; // Spring应用上下文环境 /*
*
* 实现了ApplicationContextAware 接口,必须实现该方法;
*
* 通过传递applicationContext参数初始化成员变量applicationContext
*/ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
} public static ApplicationContext getApplicationContext() {
return applicationContext;
} @SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
return (T) applicationContext.getBean(name);
} }
实现ApplicationContextAware接口时,获取ApplicationContext为null的更多相关文章
- Spring中为什么继承了ApplicationContextAware接口就可以使用ApplicationContext对象?
1.Spring中使用applicationContext对象 public class SpringContextUtil implements ApplicationContextAware { ...
- @SpringContext通过实现ApplicationContextAware接口动态获取bean
场景: 在代码中需要动态获取spring管理的bean 目前遇到的主要有两种场景:1.在工具类中需要调用某一个Service完成某一个功能,如DictUtils2.在实现了Runnable接口的任务类 ...
- 【ApplicationContext】通过实现ApplicationContextAware接口获取bean
SpringApplicationUtils.java import org.springframework.beans.BeansException; import org.springframew ...
- Spring - 运行时获取bean(ApplicationContextAware接口)
默认情况下,我们的bean都是单例模式(即从容器初始化到销毁只保持一个实例).当一个bean需要引用另外一个bean,我们往往会通过bean属性的方式通过依赖注入来引用另外一个bean.那么问题就来了 ...
- ssh下:系统初始化实现ServletContextListener接口时,获取spring中数据层对象无效的问题
想要实现的功能:SSH环境下,数据层都交由Spring管理:在服务启动时,将数据库中的一些数据加载到ServletContext中缓存起来. 系统初始化类需要实现两个接口: ServletContex ...
- spring获取ApplicationContext对象的方法——ApplicationContextAware
一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...
- 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)
ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述: 即是说,当一个类实现了这个接口之 ...
- Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式
转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236 Spring获取ApplicationContex ...
- Spring初始化ApplicationContext为null
1. ApplicationContextAware初始化 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationConte ...
随机推荐
- 转:【总结】浏览器CSS Hacks汇总,浏览器兼容方式CSS Hacks
[总结]浏览器CSS Hacks汇总 浏览器兼容可以说是前端开发所要面对的第一个挑战,目前我的电脑上已经安装了6种浏览器(基于IE内核的不算,如Maxthon等). CSS hacks利用浏览器的 ...
- nginx入门篇----负载均衡策略
负载均衡策略 负载均衡策略:内置策略和扩展策略. 内置策略包括:轮询.加权轮询.IP hash:扩展策略包括:url hash.fair等 策略详细介绍 轮询:对前端的访问逐一分流到后端网络节点,类似 ...
- fastdfs 安装配置
介绍文档 1.安装 # wget # tar xf download # cd FastDFS/ # ./make.sh 运行文件 有报错信息 compilation terminated. ...
- Keepalived+Nginx架构整理版
Keepalived介绍 keepalived是一个类似于layer3, 4, 5 交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器的状态,如 ...
- python3.5学习笔记--一个简单的图片爬虫
参考资料:http://v.qq.com/boke/page/q/g/t/q01713cvdgt.html 目的:爬取网站图片 实际上以上链接的视频中已经将整个过程说的非常明白了,稍微有点计算机基础的 ...
- asp.net教程:GridView导出到Excel或Word文件
asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...
- 一种扩大View点击范围的方法
Rect rect = new Rect();mBt0.getHitRect(rect); rect.bottom += 400; TouchDelegate touchDelegate = new ...
- Android学习笔记(十三)
Android中的广播机制 Android提供了一套完整的API,允许应用程序自由地发送和接受广播. 发送广播的方法借助于Intent,接受广播的方法需要广播接收器(BroadcastsReceive ...
- some code of c
// // main.c // LineList // // Created by Rubert on 16/9/11. // Copyright © 2016年 Study. All rights ...
- iOS开发 - OC - 实现本地数据存储的几种方式一
iOS常用的存储方式介绍 在iOS App开发过程中经常需要操作一些需要持续性保留的数据,比如用户对于App的相关设置.需要在本地缓存的数据等等.本文针对OC中经常使用的一下存储方式做了个整理. 常用 ...