实现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 ...
随机推荐
- 初学Laravel
之前一直用开tp和ot,本来觉得学会一个tp便可走遍天下,tp的确强大.但后来听到很多同行的同学说他们的公司都开始转型往lv走了,我的同学没有学过lv,然而公司给足时间去让他们去学.当然,缺人可能是占 ...
- sql中的跨库查询
在sql查询时,需要关联2个服务器上的不同数据库,只需要在所需查询的表名前加上服务器地址即可. 例如:在 192.168.0.15,8020的db110库 和 192.168.0.150,8082的d ...
- CSS盒子模型的一些理解
盒子模型相当于把现实中的盒子形象化. 盒子模型的大小="内容(content)+内填充(padding)+边框(border)+外边距(margin)" 盒子模型方向为:top, ...
- Ajax - ASP.NET MVC 4 系列
ASP.NET MVC 框架中包含一组 Ajax 辅助方法,可以用来创建表单和指向控制器操作的链接,它们是异步的,且不用编写任何脚本代码来实现程序的异步性,但需要引入脚本文件 jquer ...
- Centos7 修改ssh 默认端口号
第一步.修改/etc/ssh/sshd_config #Port 22 //这行去掉#号 Port 3600 //将端口号改成3600 第二步:在使用SELinux的系统中,需要修改SELinux 使 ...
- CocoaPods使用详细说明
使用说明: 原文:http://blog.csdn.net/lizhongfu2013/article/details/26384029 http://blog.csdn.net/showhillle ...
- 全景VR视频游戏外包公司:技术分享使用U3D+CB制作VR游戏
随着Oculus宣布1月6日开启预售,2016年很可能成为VR游戏元年,但很多的调研显示,手游设备才是市场增长的关键,SuperData发布的报告显示,2016年全球VR游戏市场规模预计在51亿美元左 ...
- php curl ftp上传 下载
下载 //初始化 $ch=curl_init(); //文件的准确路径url curl_setopt($ch,CURLOPT_URL,"ftp://192.168.151.126/wwwro ...
- IE8、IE9浏览器下报:JSON未定义 解决方法
IE8.IE9浏览器下报:JSON未定义的问题 解决方法: 在jsp中引入如下代码 <!-- 解决 IE8.IE9 下显示混乱的问题--><% String browserStrin ...
- Android学习笔记(十三)
Android中的广播机制 Android提供了一套完整的API,允许应用程序自由地发送和接受广播. 发送广播的方法借助于Intent,接受广播的方法需要广播接收器(BroadcastsReceive ...