Spring中ApplicationContextAware接口的使用
来源:互联网 发布:网络列头柜 编辑:程序博客网 时间:2024/05/15 01:39
一、这个接口有什么用?
当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。
二、怎么使用这个接口?
例如我有一个方法类UserUtil,这个方法类中需要使用到的ApplicationContext中的某个bean(appService)。
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:application.xml</param-value> </context-param>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/app-context.xml</param-value></context-param>
注意:<param-name>contextConfigLocation</param-name>是不能改变的。2、方法类UserUtil的处理
(1)实现ApplicationContextAware接口:
public class UserUtil implements ApplicationContextAware
(2)为方法类UserUtil增加一个静态的成员ApplicationContext类型的对象。以后方法类AppUtil获取ApplicationContext,就是通过读取这个成员变量的。具体如下所示:
private ApplicationContext appContext;
实现ApplicationContextAware接口的默认方法:
public void setApplicationContext(ApplicationContext paramApplicationContext) throws BeansException{ appContext = paramApplicationContext;}
(3)在spring的配置文件中,注册方法类UserUtil
严格上来说,方法类UserUtil是一个bean,而且从步骤(2)中我们不难发现,之所以方法类UserUtil能够灵活自如地获取ApplicationContext就是因为spring能够为我们自动地执行了setApplicationContext。但是,spring不会无缘无故地为某个类执行它的方法的,所以,就很有必要通过注册方法类AppUtil的方式告知spring有这样子一个类的存在。其实,方法很简单,就是将方法类AppUtil作为一个普通的bean在spring的配置文件中进行注册:
<bean id="userUtil" class="com.test.core.util.UserUtil"/>
(4)使用静态的成员ApplicationContext类型的对象,appContext,来调用其他bean。
AppService localAppService = (AppService)appContext.getBean("appService");
注:配置文件中注入AppService
<bean id="appService" class="com.test.service.AppService"> ...</bean>
也可以直接去组件扫描, service类上面加注解:@Service<!-- 组件扫描 --> <context:component-scan base-package="com.test.service"> </context:component-scan>
@Servicepublic class AppService
0 0
- Spring中ApplicationContextAware接口的使用
- Spring中ApplicationContextAware接口使用理解
- spring中ApplicationContextAware接口
- Spring中ApplicationContextAware接口的作用
- Spring中ApplicationContextAware接口的用法
- Spring中的ApplicationContextAware接口的使用
- ApplicationContextAware接口的使用
- Spring中ApplicationContextAware接口用法
- spring中ApplicationContextAware接口ServletContextAware接口
- Spring接口ApplicationContextAware介绍及使用
- Spring中ApplicationContextAware的理解
- Spring中ApplicationContextAware的作用
- Spring spring中InitializingBean接口和ApplicationContextAware接口
- spring ApplicationContextAware接口介绍
- Spring ApplicationContextAware接口
- ApplicationContextAware接口使用实例
- web 工程中利用Spring的 ApplicationContextAware接口自动注入bean
- Spring中通过实现ApplicationContextAware接口获取bean
- 守护进程
- opengl随机颜色
- Android内存泄漏原因
- faster r-cnn使用Pascal VOC2007+2012联合训练
- IE的304缓存bug
- Spring中ApplicationContextAware接口的使用
- 2.软件测试面试干货
- System.Security.Cryptography.CryptographicException: 密钥集不存在。
- java.util.ResourceBundle读取properties文件
- Google 发布的15个 Android 性能优化典范(一)
- 后 ImageNet 时代:李飞飞视觉基因组重磅计划,新一轮竞赛(HR)
- [模拟]2000年分区联赛普级组之一 计算器的改良
- Java中如何动态创建接口的实现
- 论文进度 5.11