Spring---ApplicationContext的事件机制

Spring-ApplicationContext的事件机制

事件源:ApplicationContext。publishEvent()方法:用于主动触发容器事件。

事件:ApplicationEvent类,容器事件,必须由ApplicationContext发布。

事件监听器:ApplicationListener接口,可由容器中任何监听器Bean担任。onApplicationEvent(ApplicationEvent event):每当容器内发生任何事件时,此方法都被触发

容器事件类需继承ApplicationEvent类,容器事件的监听器类需实现ApplicationListener接口。

下面给出一个例子(防止看我博客的小白错误包的导入 加上了包代码)

1.首先定义一个EmailEvent类,继承ApplicationEvent类

import org.springframework.context.ApplicationEvent;

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类,实现ApplicationListener接口,并复写onApplicationEvent()方法

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

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());
        } else {
            System.out.println("容器内部事件" + event);
        }
    }

}

3.将监听器配置在Spring容器中(即Spring配置文件中)—-(这里需要注意容器中所配置的监听器的类路径,你的应该和我的不同。不要盲目复制)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans                  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    <bean class="com.book.spring.EmailNotifier"/>
</beans>

4.编写主程序main

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringText {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/bean.xml");
        EmailEvent ele = new EmailEvent("test", "spring_test@163.com", "this is a test");
        ctx.publishEvent(ele);
    }

}

程序到此结束,运行结果如下:

容器本身的事件:
org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@b81eda8: startup date [Thu Dec 04 20:20:52 CST 2014]; root of context hierarchy]
发送邮件的接收地址spring_test@163.com
发送邮件的内容this is a test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring-beans.jar是Spring框架中的一个核心JAR文件,它包含了Spring容器的基本组件BeanFactory和ApplicationContext,以及与Bean相关的一系列类和接口。如果没有spring-beans.jar,Spring框架将不能运行。 首先,Spring框架的核心思想是依赖注入(Dependency Injection,DI)和控制反转(Inversion of Control,IOC)。这意味着对象之间的依赖关系将不再由对象自身管理,而是由Spring容器来管理。在这种情况下,Spring容器必须能够创建和管理对象的实例,以及管理它们之间的依赖关系。这就是spring-beans.jar的作用。 BeanFactory是Spring容器的基本组件之一。它是一个工厂类,负责管理Bean的生命周期、创建Bean实例、管理Bean的依赖关系和Bean之间的通信。ApplicationContext是BeanFactory的扩展,除了BeanFactory的功能,它还提供了事件机制、AOP(面向切面编程)、异常处理等其他的功能。Spring框架的大部分应用程序都需要使用BeanFactory或ApplicationContext来管理Bean。 另外,spring-beans.jar还包含了一系列与Bean相关的类和接口。例如,BeanInfo、BeanDefinition、BeanWrapper、BeanPostProcessor、InitializingBean、DisposableBean等,这些类和接口提供了在Bean创建、初始化、销毁等过程中的扩展点,可以让开发者在这些过程中加入自己的逻辑,实现自己的扩展。 总之,spring-beans.jar可以说是Spring框架的核心之一,没有它,Spring框架将不能运行。它提供了创建、管理Bean实例以及它们之间的依赖关系、扩展点等功能,是Spring框架中不可替代的重要组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值