首页 > 其他分享 >springboot-监听器

springboot-监听器

时间:2023-04-02 11:47:18浏览次数:36  
标签:springboot boot springframework event context org 监听器

监听器

  • ApplicationListener可以实现这个接口时传入对应的监听器,用于监听该事件
  • 比如:实现 ApplicationListener<ContextRefreshedEvent> 接口,重写 onApplicationEvent 方法,将 ContextRefreshedEvent 对象传进去。如果我们想在加载或刷新应用上下文时,也重新刷新下我们预加载的资源,就可以通过监听 ContextRefreshedEvent 来做这样的事情。
package cn.tjhis.listener;

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

/**
 * 描述 : 在这里配置 \META-INF\spring.factories
 * 1 org.springframework.boot.context.event.ApplicationStartingEvent
 * 2 org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
 * 3 org.springframework.boot.context.event.ApplicationContextInitializedEvent
 * 4 org.springframework.boot.context.event.ApplicationPreparedEvent
 * 5 org.springframework.context.event.ContextRefreshedEvent
 * 6 org.springframework.boot.context.event.ApplicationStartedEvent
 * 7 org.springframework.boot.availability.AvailabilityChangeEvent
 * 8 org.springframework.boot.context.event.ApplicationReadyEvent
 * 9 org.springframework.boot.availability.AvailabilityChangeEvent
 *
 * <p>路径 : cn.tjhis.listener
 *
 * <p>工程 : autobean
 *
 * <p>作者 : wanghx
 *
 * <p>日期 : 2023-04-02 11:17
 *
 * @author : Administrator
 */
public class MyListener implements ApplicationListener {
    private int count=0;
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        count++;
        System.out.printf("监听到事件: %5d %-1s",count,event.getClass().getName());
        System.out.println("");
    }
}

标签:springboot,boot,springframework,event,context,org,监听器
From: https://www.cnblogs.com/his365/p/17280146.html

相关文章

  • SpringBoot中Mybatis的应用
    创建一个SpringBoot项目,然后如下操作:(1)添加Lombok插件--简化实体类 (2)添加Mybatis框架和MySQL驱动---访问Mysql和使用Mybatis (3)创建pojo包和实体类在项目中创建pojo包,并在其中创建实体类。实体类上可以使用Lombok注解。首次使用时,需要在Ieda中安装该插件。L......
  • SpringBoot定时任务
    使用注解@Scheduled1.在启动类上添加注解@EnableScheduling开启定时任务2.创建定时任务@ComponentpublicclassStatisticsComp{/***日统计(每日0点1分触发)*/@Scheduled(cron="010?**")publicvoiddailyStatistics(){}/**......
  • springboot-自己开发start
    步骤命名规范第三方在建立自己的Starter的时候命名规则统一用xxx-spring-boot-starter,官方提供的Starter统一命名方式为spring-boot-starter-xxx。步骤新建一个Maven项目,在pom.xml文件中定义好所需依赖;新建配置类,写好配置项和默认值,使用@ConfigurationProperties指明......
  • 【SpringBoot】关闭SpringBoot启动图标(banner)
    SpringBoot启动的时候会有如下图标如果想去掉此图标在配置文件添加一下内容配置文件:application.yml添加内容:spring:main:banner-mode:off#关闭SpringBoot启动图标(banner) ......
  • 常用注解-SpringBoot请求
    SpringBoot请求常用注解及作用范围:@Controller:【类】需要返回一个视图(themleaf),加注解4@ResponseBody等于注解2@RestController:【类】返回字符串等,与注解1都属于控制器,@RequestMapping:【方法/类】设置方法或者类的请求地址,@ResponseBody:【方法】支持将返回值放在response......
  • SpringBoot进阶教程(七十五)数据脱敏
    无论对于什么业务来说,用户数据信息的安全性无疑都是非常重要的。尤其是在数字经济大火背景下,数据的安全性就显得更加重要。数据脱敏可以分为两个部分,一个是DB层面,防止DB数据泄露,暴露用户信息;一个是接口层面,有些UI展示需要数据脱敏,防止用户信息被人刷走了。v需求背景DB层面的......
  • SpringBoot项目启动时初始化操作方式
    1.实现InitializingBean重写afterPropertiesSet()方法。@Component@Slf4jpublicclassInitOneTestimplementsInitializingBean{@OverridepublicvoidafterPropertiesSet()throwsException{log.info("InitOneTestinitsuccess");}}2......
  • springboot和redis执行lua脚本——踩坑
    问题:原先想使用redis执行lua脚本作为项目限流基础,lua脚本后写完后执行一直报错如下图:  卡了几天了,没看明白咋回事,一次偶然试了一下解决了,传递lua参数需要时String类型难怪说报错强转String类型异常  灵感来源参考文章:踩坑之RedisTemplate执行Lua脚本-知乎(zhihu.c......
  • Springboot 系列 (26) - Springboot+HBase 大数据存储(四)| Springboot 项目通过 HBase
    ApacheHBase是Java语言编写的一款Apache开源的NoSQL型数据库,不支持SQL,不支持事务,不支持Join操作,没有表关系。ApacheHBase构建在ApacheHadoop和ApacheZookeeper之上。ApacheHBase:https://hbase.apache.org/HBase的安装配置,请参考“Springboot系列(24)-......
  • VUE分别使用普通方法、计算属性、监听器完成简易计算器
    VUE分别使用普通方法、计算属性、监听器完成简易计算器声明:本方法使用VUE完整框架独立模块组件来实现TOP:实现效果Ⅰ:完整框架Ⅱ:框架实现案例组件功能细分1.APP组件总组件,管理所有组件(每个单独的组件最后都汇总到APP组件里,便于管理)管理汇总:Methodss组件、Watchss......