ApplicationContextAware接口的作用
1/==============
在Web应用中,Spring容器通常采用声明式方式配置产生:开发者只要在web.xml中配置一个Listener,该Listener将会负责初始化Spring容器,MVC框架可以直接调用Spring容器中的Bean,无需访问Spring容器本身。在这种情况下,容器中的Bean处于容器管理下,无需主动访问容器,只需接受容器的依赖注入即可。
但在某些特殊的情况下,Bean需要实现某个功能,但该功能必须借助于Spring容器才能实现,此时就必须让该Bean先获取Spring容器,然后借助于Spring容器实现该功能。为了让Bean获取它所在的Spring容器,可以让该Bean实现ApplicationContextAware接口。
下面示例为实现ApplicationContextAware的工具类,可以通过其它类引用它以操作spring容器及其中的Bean实例。
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
/**
* 获取静态变量中的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() {
applicationContext = null;
}
/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext = applicationContext;
}
/**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected() {
Validate.validState(applicationContext != null,
"applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder.");
}
}
Spring容器会检测容器中的所有Bean,如果发现某个Bean实现了ApplicationContextAware接口,Spring容器会在创建该Bean之后,自动调用该Bean的setApplicationContextAware()方法,调用该方法时,会将容器本身作为参数传给该方法——该方法中的实现部分将Spring传入的参数(容器本身)赋给该类对象的applicationContext实例变量,因此接下来可以通过该applicationContext实例变量来访问容器本身。
例如:
在CacheUtil中通过spring容器创建CacheManager实例
public class CacheUtils {
private static CacheManager cacheManager = ((CacheManager) SpringContextHolder.getBean("cacheManager"));
}
2/==========================
一、这个接口有什么用?
当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。
二、怎么用?
举个例子吧:
例如我有一个方法类AppUtil,这个方法类中需要使用到的ApplicationContext中的某个bean(companyService)。
1、因为spring要建立属于自己的容器,就必须要加载自己的配置文件。
这个时候,需要注册ContextLoaderListener或者这个类的子类。
在web.xml加上以下的信息:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
当然,这样子的话只会读取默认路径下的application.xml配置文件的。如果需要读取特定路径下的配置文件。需要在web.xml中
添加如下信息。可以参考我的示例,指定配置文件,如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/app-context.xml</param-value>
</context-param>
注意:<param-name>contextConfigLocation</param-name>是不能改变的。
2、方法类AppUtil的处理
方法类AppUtil实现ApplicationContextAware接口:
public class AppUtil
implements ApplicationContextAware
为方法类AppUtil增加一个静态的成员ApplicationContext类型的对象。以后方法类AppUtil获取ApplicationContext,就是通过读取这个
成员变量的。具体如下所示:
private static ApplicationContext appContext;
实现ApplicationContextAware接口的默认方法:
public void setApplicationContext(ApplicationContext paramApplicationContext)
throws BeansException
{
appContext = paramApplicationContext;
}
3、在spring的配置文件中,注册方法类AppUtil
严格上来说,方法类AppUtil是一个bean,而且从步骤2中我们不难发现,之所以方法类AppUtil能够灵活自如地获取ApplicationContext
就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以,就很有必要
通过注册方法类AppUtil的方式告知spring有这样子一个类的存在。
(加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的
public void setApplicationContext(ApplicationContext context) throws BeansException
方法,获得ApplicationContext对象,ApplicationContext对象是由spring注入的。前提必须在Spring配置文件中指定该类)
其实,方法很简单,就是将方法类AppUtil作为一个普通的bean在spring的配置文件中进行注册:
<bean id="appUtil" class="com.htsoft.core.util.AppUtil"/>
4、使用静态的成员ApplicationContext类型的对象,appContext,来调用其他bean。在方法类AppUtil中增加如下方法:
public static Object getBean(String paramString)
{
return appContext.getBean(paramString);
}
那么,在
方法类AppUtil中就能够灵活地调用其他任何一个bean了,例如:
CompanyService localCompanyService = (CompanyService)getBean("companyService");
注:配置文件中关于companyService的内容:
<bean id="companyService" class="com.kaiwii.service.system.impl.CompanyServiceImpl">
<constructor-arg index="0" ref="companyDao"/>
</bean>
ApplicationContextAware接口的作用的更多相关文章
- ApplicationContextAware 接口的作用
接口说明:当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean.换句话说,就是这个类可以直接获取Spring配置文件中,所有有引用到的bean对象 ...
- Spring的ApplicationContextAware接口的作用
ApplicationContextAware接口: 当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean.换句话说,就是这个类可以直接获取Spr ...
- 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)
ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述: 即是说,当一个类实现了这个接口之 ...
- Spring中ApplicationContextAware接口的说明
转载 https://www.cnblogs.com/muqingzhi123/p/9805623.html 1.为什么使用AppplicationContextAware? ApplicationC ...
- Spring中ApplicationContextAware接口的用法
1.为什么使用AppplicationContextAware? ApplicationContext的BeanFactory 的子类, 拥有更强大的功能,ApplicationContext可以在服 ...
- java 接口的作用和好处
1.java 接口的作用 http://blog.csdn.net/hack_bug/article/details/7634737 2.一位Java大牛的回答 很多JAVA初级程序员对于接口存在的意 ...
- C#接口的作用(经典)
C#接口是一个让很多初学C#者容易迷糊的东西,用起来好像很简单,定义接口,里面包含方法,但没有方法具体实现的代码,然后在继承该接口的类里面要实现接口的所有方法的代码,但没有真正认识到接口的作用的时候就 ...
- ApplicationContextAware 接口
一.这个接口有什么用? 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以 ...
- 实现ApplicationContextAware接口时,获取ApplicationContext为null
将懒加载关闭,@Lazy(false),默认为true import org.springframework.beans.BeansException; import org.springframew ...
随机推荐
- windows系统安装ubuntu双系统
近期由于要学习python开发,经常需要用到linux环境.但是一般的编辑和办公在windows环境下有非常的舒服,所以就想装一个双系统.经过几经周折,终于在我的系统上装成功了,在这分享一些安装过程. ...
- fork failed.: Cannot allocate memory
在做压力测试时候: [root@666 ok]# webbench -c 5000 -t30 http://10.100.0.61/ Webbench - Simple Web Benchmark 1 ...
- centos 7 五笔安装
# yum install ibus ibus-table-wubi 需要重启动
- git将本地项目添加到github上
git init git add . git commit -m '添加备注' git add remote origin originUrl git push -u origin master 注意 ...
- lua -- io.exists
io.exists 测试文件是否存在,如果存在返回 true. 格式: result = io.exists(路径) 可以使用 CCFileUtils:fullPathForFilename() 函数 ...
- windows远程连接mac配置方法
1.开启mac共享服务,设置通过密码进行连接 [系统偏好设置]中选择[共享]打开[屏幕共享]服务,即可允许其他电脑的用户远程查看并控制此电脑.点击[电脑设置]配置访问密码 2. 下载TightVNC, ...
- 每日英语:Redfin Real-Estate Firm Gets Cold Shoulder in Silicon Valley
"I used to think I was this made man," says entrepreneur Glenn Kelman. "That's what t ...
- 每日英语:Auto Makers Accelerate Efforts to Develop Self-Driving Cars
Big auto makers are steering their efforts to develop cars that drive themselves out of the labs and ...
- linux命令(47):Linux下对文件进行按行排序,去除重复行
Linux下对文件进行按行排序:sort 与 uniq 命令简介 Linux | May 24, 2015 | linux sort 命令可针对文本文件的内容,以行为单位进行排序.其基本语法格式为: ...
- git push remote error解决办法
通常在用git clone了remote端(服务器)的git仓库后,再进行了自己一系列修改后,会将自己测试后稳定的状态push到remote端,以更新源仓库,使 其他人在pull的时候得到自己的修改. ...