ApplicationAware

加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的

方法,获得ApplicationContext对象。

前提必须在Spring配置文件中指定该类

获取context后就可以拿到容器中值

ApplicationEvent

是个抽象类,里面只有一个构造函数和一个长整型的timestamp。

ApplicationListener

是一个接口,里面只有一个onApplicationEvent方法。在使用时 要判断事件

所以自己的类在实现该接口的时候,要实装该方法。

ApplicationContextAware接口可以实现我们在初始化bean的时候给bean注入ApplicationConxt(Spring上下文对象)对象。

ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播。通过ApplicationContextAware我们可以把系统中所有ApplicationEvent传播给系统中所有的ApplicationListener。因此,我们只需要构造好我们自己的ApplicationEvent和ApplicationListener,就可以在系统中实现相应的监听器。

如果只实现监听,代码会在项目启动后执行。

//实现事件
import org.springframework.context.ApplicationEvent; public class PersonEvent extends ApplicationEvent {
/**
* <p>Description:</p>
*/
private static final long serialVersionUID = 1L;
public String address;
public String text; public PersonEvent(Object source) {
super(source);
} public PersonEvent(Object source, String address, String text) {
super(source);
this.address = address;
this.text = text;
} public void print(){
System.out.println("hello spring event!");
} }
//实现接口监听 注入事件
@Component
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; public class PersonListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof EmailEvent){
PersonEvent personEvent= (PersonEvent)event;
personEvent.print();
System.out.println("the source is:"+ personEvent.getSource());
System.out.println("the address is:"+ personEvent.address);
System.out.println("the email's context is:"+ personEvent.text);
} } }
//测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
PersonEvent event = new PersonEvent("hello","test@163.com","this is a email text!");
context.publishEvent(event);
}
}


Spring中application*的使用的更多相关文章

  1. Velocity初探小结--Velocity在spring中的配置和使用

    最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...

  2. Spring中Bean的实例化

                                    Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...

  3. Spring中文文档

    前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...

  4. [原创]java WEB学习笔记109:Spring学习---spring中事物管理

    博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好 ...

  5. Spring中使用Schedule调度

    在spring中两种办法使用调度,以下使用是在spring4.0中. 一.基于application配置文件,配置入下: <bean id="jobDetail" class ...

  6. ssh下:系统初始化实现ServletContextListener接口时,获取spring中数据层对象无效的问题

    想要实现的功能:SSH环境下,数据层都交由Spring管理:在服务启动时,将数据库中的一些数据加载到ServletContext中缓存起来. 系统初始化类需要实现两个接口: ServletContex ...

  7. Spring中Aware相关接口原理

    Spring中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.ServletContextA ...

  8. spring中的Log4jConfigListener作用和webapp.root的设置

    转:http://blog.sina.com.cn/s/blog_7bbf356c01016wld.html 使用spring中的Log4jConfigListener有如如下好处:     1. 动 ...

  9. 如何使用spring中的Log4jConfigListener--删除

    使用spring中的Log4jConfigListener有如如下好处:    1. 动态的改变记录级别和策略,不需要重启Web应用,如<Effective Enterprise Java> ...

随机推荐

  1. Java基础面试题 (一)

    1.面向对象的三个特征 封装,继承,多态.这个应该是人人皆知,有时候也会加上抽象. 2.多态的好处 允许不同类对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式(发送消息 ...

  2. 深入理解Feign之源码解析

    转载请标明出处: 本文出自方志朋的博客 什么是Feign Feign是受到Retrofit,JAXRS-2.0和WebSocket的影响,它是一个jav的到http客户端绑定的开源项目. Feign的 ...

  3. [置顶] Retrofit2 初印象?

    鄙人由于工作繁忙很久没写博客了还望大家谅解!之前csdn登不上,算了不说借口了,retrofit2相信已经很火了吧,而且上手也比较容易,之前可能大家都是用Volley,Okhttp.Okhttp3其实 ...

  4. [转载][QT][SQL]sql学习记录5_sqlite视图(View)

    转载自:http://www.runoob.com/sqlite/sqlite-view.html SQLite 视图(View) 视图(View)只不过是通过相关的名称存储在数据库中的一个 SQLi ...

  5. php 配置上传大文件

    打开php.ini,首先找到file_uploads = on ;是否允许通过HTTP上传文件的开关.默认为ON即是开upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就 ...

  6. Linux OpenCV 静态链接错误

    错误一: undefined reference to `dlopen' undefined reference to `dlerror' undefined reference to `dlsym' ...

  7. C的动态链表建立

    运用到的函数为: 动态内存分配函数malloc()              比如:char *name=(char *)malloc(20);  相当与c++的new关键字 动态内存释放函数free ...

  8. 《DSP using MATLAB》示例Example 7.13

    代码: M = 25; alpha = (M-1)/2; n = [0:1:M-1]; hd = (2/pi) * ( (sin( (pi/2)*(n-alpha) ).^2)./(n-alpha) ...

  9. http状态码status

    status——http状态码 1xx 消息 2xx 成功 3xx 重定向 ▪ 301 Moved Permanently 永久重定向——下回不会再找他了 ▪ 302 Move temporarily ...

  10. normalizr api 转换类库使用

    1. 项目初始化 yarn init yarn add normalizr 项目结构 app.js package.json user.json 2. 使用 a. app.js const userj ...