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接 ...
随机推荐
- 网页播放器(jsp、js)
jsp对控件显示 <%@ page language="java" import="java.util.*" pageEncoding="UTF ...
- linux-14基础命令之-复制(cp)移动(mv),删除(rm),拷贝文件(dd)
1.cp 命令用于复制文件或者目录 格式为:cp[选项]源文件 目标文件 复制名录有三种情况: @1.目标文件是一个目录,将源复制到该目录下: @2.目标文件是一个文件,将源文件覆盖该文件: @3 ...
- WebView一般用法总结
下面是webview常规的用法: import android.annotation.SuppressLint;import android.app.Activity;import android.o ...
- Android网页中tel,sms,mailTo,Intent,Market协议用法总结
tel:协议---拨打电话 <a href="tel:">调出拨号界面</a> <a href="tel:10086">调 ...
- TypeScript之基本数据类型
前言 最近项目很急,所以没有什么时间回答关于Xamarin.Android方面的问题,也有一段时间没有更新.主要是手头很缺人,如果有谁有兴趣加入我们的话,可以私聊我,这样我就能继续造福社区了,同时还有 ...
- 生产环境下实践DDD中的规约模式
最近的开发工作涉及到两个模块“任务”和“日周报”.关系是日周报消费任务,因为用户在写日周报的时候,需要按一定的规则筛选当前用户的任务,作为日周报的一部分提交.整个项目采用类似于Orchard那种平台加 ...
- Java设计模式2:简单工厂模式
简单工厂模式 简单工厂模式是类的创建模式,又叫做静态工厂方法模式.简单工厂模式由一个工厂对象决定生产出哪一种产品类的实例. 为什么要使用简单工厂模式 原因很简单:解耦. A对象如果要调用B对象,最简单 ...
- java提高篇(二五)-----HashTable
在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key ...
- [51单片机] 以PWM控制直流电机为例建一个简单的51工程框架
目录 1)功能概述 2)引脚连接 3)框架介绍 4)模块说明 5)复用规则 6)工程链接 1)功能概述 名称:独立按键控制直流电机调速 内容:对应的电机接口需用杜邦线连接到uln2003电机控制端; ...
- 跟我一起云计算(6)——openAPI
介绍 Open API即开放API,也称开放平台. 所谓的开放API(OpenAPI)是服务型网站常见的一种应用,网站的服务商将自己的网站服务封装成一系列API(Application Program ...