Example of ApplicationContextAware in Spring--转
原文地址:http://www.concretepage.com/spring/example_applicationcontextaware_spring
In spring we can get ApplicationContext anywhere in our code with the help of ApplicationContextAware. ApplicationContextAware is the interface and there is only one method setApplicationContext() in it. When a class implements ApplicationContextAware, that class needs to override the setApplicationContext() method. ApplicationContextAware is used for bean lookup purpose and for those objects which needs to access file resources. Find the example below.
Implement ApplicationContextAware Interface
ApplicationContextAwareTest.java
package com.concretepage;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextAwareTest implements ApplicationContextAware {
ApplicationContext context;
public ApplicationContext getContext() {
return context;
}
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context=context;
}
}
Configure Bean
app-conf.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <bean id="testA" class="com.concretepage.A"/>
<bean id="appcontext" class="com.concretepage.ApplicationContextAwareTest"/>
</beans>
A.java
package com.concretepage;
public class A {
public void doTask(){
System.out.println("Do some task.");
}
}
Run Demo
SpringTest.java
package com.concretepage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-conf.xml");
ApplicationContextAwareTest appcontext= (ApplicationContextAwareTest)context.getBean("appcontext");
ApplicationContext appCon =appcontext.getContext();
A a= (A)appCon.getBean("testA");
a.doTask();
context.registerShutdownHook();
}
}
Find the output.
Do some task.
Example of ApplicationContextAware in Spring--转的更多相关文章
- implements ApplicationContextAware 获取spring 容器
1.新建 ApplicationContextUtil 类 ,通过实现 ApplicationContextAware 的 setApplicationContext 方法,得到context上下文: ...
- 【11-10】spring学习笔记-ApplicationContextAware
package util; /** * @author aloha_world_ * @date 2016年11月10日 下午7:50:08 * @version v1.00 * @descripti ...
- web 工程中利用Spring的 ApplicationContextAware接口自动注入bean
最常用的办法就是用 ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApp ...
- Spring中ApplicationContextAware接口的说明
转载 https://www.cnblogs.com/muqingzhi123/p/9805623.html 1.为什么使用AppplicationContextAware? ApplicationC ...
- Spring中ApplicationContextAware接口的用法
1.为什么使用AppplicationContextAware? ApplicationContext的BeanFactory 的子类, 拥有更强大的功能,ApplicationContext可以在服 ...
- Spring的ApplicationEvent实现
原理:ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播.通过ApplicationCont ...
- 深入Spring:自定义注解加载和使用
前言 在工作中经常使用Spring的相关框架,免不了去看一下Spring的实现方法,了解一下Spring内部的处理逻辑.特别是开发Web应用时,我们会频繁的定义@Controller,@Service ...
- spring里头各种获取ApplicationContext的方法
为啥写这个文章呢?spring各个版本不同,以及和系统框架套在一起不同,导致获取的方式不同,网络上各种版本,太乱了,写获取方式的人都不写这个获取方式是在本地还是在WEB,在那种应用服务器下,在spri ...
- Spring的Bean的生命周期
一:生命周期执行的过程如下:1) spring对bean进行实例化,默认bean是单例.2) spring对bean进行依赖注入.3) 如果bean实现了BeanNameAware接口,spring将 ...
- springBean生命周期----来自spring实战总结
1.Spring对bean进行实例化 2.Spring将值和bean的引用注入到bean对应的属性中(比如说注入到被依赖的bean的方法中或属性里) 3.如果bean实现了BeanNameAware接 ...
随机推荐
- 三星(SAMSUNG)910S3L-K04 安装win7的BIOS设置
三星(SAMSUNG)910S3L-K04 开机后连续点击F2进入BIOS,再进入BOOT.将SECURE BOOT CONTROL点成disabled,将OS MODE SELECTION选为uef ...
- 用Navicat Premium 远程连接oracle数据库
1.安装Navicat Premium软件(我的是11.0.7版本)(假设安装路径为D:\NavicatLite\Navicat Premium) 2.下载 instantclient-basic-n ...
- .net 第二周学习
这周更进一步的介绍了.net,通过作业的练习,有那么一点点的成就感,相对于前端,成就感还不是很大,但是我还是会继续加油 学习.net,看着他们周末只能呆在寝室写网页,顿时我就高兴了: ...
- 字符串混淆技术在.NET程序保护中的应用及如何解密被混淆的字符串
Visual Studio提供的Dotfuscator保护程序,可以对用户代码中包含的字符串进行加密.比如下面的例子,为了找到这个程序的注册算法,用.NET Reflector加载程序集后,发现代码中 ...
- AMD加载器实现笔记(四)
继续这一系列的内容,到目前为止除了AMD规范中config的map.config参数外,我们已经全部支持其他属性了.这一篇文章中,我们来为增加对map的支持.同样问题,想要增加map的支持首先要知道m ...
- javascript笔记:javascript的关键所在---作用域链
javascript里的作用域是理解javascript语言的关键所在,正确使用作用域原理才能写出高效的javascript代码,很多javascript技巧也是围绕作用域进行的,今天我要总结一下关于 ...
- C#Lite Unity热更新开源解决方案改名C#Light
因为此脚本语言与C#Lite Framework 开源项目重名,故修改名称,发音不变.
- [ZigBee] 2、 ZigBee开发环境搭建
本节介绍ZigBee基本开发环境搭建,附件中有本节介绍的软件下载地址~ 链接:http://pan.baidu.com/s/1bLdqxg 1.IAR Embedded Workbench 的安装 S ...
- ReactMix框架,让你实现一套js代码,基于ReactNative在H5,App都能完美跑起来,Write Once,Run Anywhere
ReactNative框架推出已经有一段时间了,相信很多小伙伴都在尝试实现Write Once, Run Anywhere的梦想,比如淘宝的ReactWeb等等,但是这些框架都局限于因为ReactNa ...
- Lucene系列-近实时搜索(1)
近实时搜索(near-real-time)可以搜索IndexWriter还未commit的内容,介于immediate和eventual之间,在数据比较大.更新较频繁的情况下使用.本文主要来介绍下如何 ...