spring事件(一)--内置事件

spring事件(一)--内置事件
摘要由CSDN通过智能技术生成

一、简介

spring框架是java开发中常用的,通常我们需要在spring的某些事件发生时,做某些操作。

在此介绍spring的内置事件,以父抽象类ApplicationContextEvent下的四个子事件类为主。

类图如下:


二、事件类

1、ApplicationContextEvent

是spring内置事件的父抽象类,构造方法传入spring的context容器,同时也有获取spring的context容器的方法。

源码如下:

/**
 * Base class for events raised for an {
   @code ApplicationContext}.
 *
 * @author Juergen Hoeller
 * @since 2.5
 */
@SuppressWarnings("serial")
public abstract class ApplicationContextEvent extends ApplicationEvent {

   /**
    * Create a new ContextStartedEvent.
    * @param source the {
   @code ApplicationContext} that the event is raised for
    * (must not be {
   @code null})
    */
   public ApplicationContextEvent(ApplicationContext source) {
      super(source);
   }

   /**
    * Get the {
   @code ApplicationContext} that the event was raised for.
    */
   public final ApplicationContext getApplicationContext() {
      return (ApplicationContext) getSource();
   }

}
2、ContextRefreshedEvent

当spring容器初始化或刷新时,会触发此事件。此事件在开发中常用,用于在spring容器启动时,导入自定义的bean实例到spring容器中。

源码如下:

/**
 * Event raised when an {
   @code ApplicationContext} gets initialized or refreshed.
 *
 * @author Juergen Hoeller
 * @since 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值