ApplicationContext的事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListerner接口来实现。

1. 创建EmailEvent

public class EmailEvent  extends ApplicationEvent{

    private String address;
private String text; public EmailEvent(Object source) {
super(source);
} public EmailEvent(Object source, String address, String text) {
super(source);
this.address = address;
this.text = text;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} }

2. 创建EmailNotifier类

public class EmailNotifier implements ApplicationListener{

    @Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof EmailEvent) {
EmailEvent emailEvent = (EmailEvent)event;
System.out.println("邮件接收地址:" + emailEvent.getAddress());
System.out.println("邮件正文: " + emailEvent.getText());
} } }

3. 创建容器配置文件  beans_mail.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="emailNotifier" class="com.tutorialspoint.EmailNotifier">
</bean> </beans>

4. 测试

ApplicationContext context = new ClassPathXmlApplicationContext("beans_mail.xml");
EmailEvent emailEvent = new EmailEvent("hello","spring@163.com","this is test");
context.publishEvent(emailEvent);

5. 输出结果

六月 01, 2016 5:13:10 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@9b6220: startup date [Wed Jun 01 17:13:10 CST 2016]; root of context hierarchy
六月 01, 2016 5:13:10 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans_mail.xml]
邮件接收地址:spring@163.com
邮件正文: this is test

Spring中的ApplicationContext事件机制的更多相关文章

  1. spring中的异步事件

    这里讲解一下Spring对异步事件机制的支持,实现方式有两种: 1.全局异步 即只要是触发事件都是以异步执行,具体配置(spring-config-register.xml)如下: Java代码   ...

  2. spring中获取applicationContext

    常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...

  3. spring中的事务传播机制

    1.事务的实现思想 在spring中要想某个方法具有事务,只要在方法前加一个@Transactional注解.然后spring就会利用aop思想,在这个方法执行前开启事务, 在方法执行后选择提交事务或 ...

  4. Spring中利用applicationContext.xml文件实例化对象和调用方法

    Spring中实例化对象和调用方法入门 1.jar包和xml的准备 已上传至百度云盘,链接: https://pan.baidu.com/s/1CY0xQq3GLK06iX7tVLnp3Q 提取码: ...

  5. 半夜思考之查漏补缺 , Spring 中的 Bean 继承机制

    这里的继承 , 不是 Java 中的继承 , 下面就总结下 Bean继承与Java继承的区别: Spring 中的子类 Bean 和父 Bean 可以是不同类型 , 但是 Java 中的继承则可保证子 ...

  6. 在Spring中使用异步事件实现同步事务

    结合Scala+Spring,我们将采取一个很简单的场景:下订单,然后发送一封电子邮件. 编制一个服务: @Serviceclass OrderService @Autowired() (orderD ...

  7. spring中自定义Event事件的使用和浅析

    在我目前接触的项目中,用到了许多spring相关的技术,框架层面的spring.spring mvc就不说了,细节上的功能也用了不少,如schedule定时任务.Filter过滤器. intercep ...

  8. [案例一] Spring中的事件驱动模型(机制)

    事件驱动模型是观察者模式的另一种形态,观察者相当于监听器,被观察者相当于事件源 事件源产生事件,监听器监听事件 以用户注册时候,要发送邮件和发送短信举例说明 定义一个事件 /** * spring会自 ...

  9. 【转载】Spring中的applicationContext.xml与SpringMVC的xxx-servlet.xml的区别

    一直搞不明白两者的区别. 如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 一 ...

随机推荐

  1. redis 配置应用(摘)

    Redis可以在没有配置文件的情况下通过内置的配置来启动,但是这种启动方式只适用于开发和测试. 合理的配置Redis的方式是提供一个Redis配置文件,这个文件通常叫做redis.conf. redi ...

  2. dataserver test code

    main.go package main import ( "database/sql" "fmt" "log" "net/htt ...

  3. laravel 的.env 配置文件

      (相关的环境变量) APP_ENV=localAPP_DEBUG=trueAPP_KEY=base64:cWMz1hM4dp9ihoOtUs6iV+BDZX9KtjYvMALFmyEPQfI= D ...

  4. 关于JAVA EE项目在WEB-INF目录下的jsp页面如何访问WebRoot中的CSS和JS文件

    找了这么久资料,总算解决了 感谢博客园:http://www.cnblogs.com/xsht/p/5275081.html 感谢百度:http://zhidao.baidu.com/link?url ...

  5. zoj 1789 The Suspects

    好高兴,又AC一道 ,不过是很类似的两道..还是好高兴呀思想跟2833是一样的,不过要重新设计输入和输出.老师上课又重新讲解了一下,因为嫌疑人已知是0,所以加入集中时应该默认让数值小的做树根,即最终让 ...

  6. Python’s SQLAlchemy vs Other ORMs[转发 5] PonyORM

    PonyORM PonyORM allows you to query the database using Python generators. These generators are trans ...

  7. 【CSS3】标签使用说明

    转换(transform):改变元素的形状.大小和位置. transform:rotate(20deg):顺时针旋转20° rotate()用来2D旋转改变角度.支持负数,表示逆时针. transfo ...

  8. @keyframes

    通过 @keyframes 规则,您能够创建动画. @keyframes movelike{ from{right:1205px;} to{right:0px}} 创建动画的原理是,将一套 CSS 样 ...

  9. 2个集合比较——最高效解法(Java实现)

    优点:时间复杂度为O(n)级别: 缺点:只适用于Int,以及Int的数字不能过大,集合元素数量不能过多. 理论分析: 两个集合的元素之和以及之积相同则,这两个集合相等.(前提是两个集合的数量一致) 证 ...

  10. MySQL:日期函数、时间函数总结

    MySQL 获得当前日期时间 函数 获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | n ...