当前位置: 首页 > news >正文

Application接口拓展功能(三)

        Application容器接口除了有之前的几个拓展功能之外,还具备事件发布的拓展能力,它起到了解耦的作用,可以把多个事情的处理独立出来。

       1.首先需自定义一个事件对象

       事件对象需继承ApplicationEvent,这里我们需要重写事件发布对象的构造方法。如果不写会报错会默认继承的构造函数,但是父类并没有无参的构造函数,所以会报错。

package com.example.springdemo.demos.a20;import org.springframework.context.ApplicationEvent;/*** @author zhou* @version 1.0* @description TODO* @date 2025/9/20 20:38*/
public class UserRegisteredEvent extends ApplicationEvent {public UserRegisteredEvent(Object source) {super(source);}
}

         2.写一个事件监听器处理事件

package com.example.springdemo.demos.a20;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;/*** @author zhou* @version 1.0* @description TODO* @date 2025/9/20 20:37*/@Component
public class Component2 {private static final Logger log = LoggerFactory.getLogger(Component2.class);@EventListenerpublic void aaa(UserRegisteredEvent event){log.debug("{}",event);}}

          3.context对象发布事件

package com.example.springdemo.demos.a20;import com.example.springdemo.demos.a19.TestFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.DefaultBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.EventPublishingRunListener;
import org.springframework.boot.env.EnvironmentPostProcessorApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;import javax.swing.*;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;/*** @author zhou* @version 1.0* @description TODO* @date 2025/5/31 14:37*/
@SpringBootApplication
public class TestApplicationContext {public static void main(String[] args) throws IOException {ConfigurableApplicationContext context = SpringApplication.run(TestApplicationContext.class, args);System.out.println(context.getMessage("hi", null, Locale.CHINA));System.out.println(context.getMessage("hi", null, Locale.ENGLISH));System.out.println(context.getMessage("hi", null, Locale.JAPANESE));//资源通配符解析org.springframework.core.io.Resource[] resources = context.getResources("classpath*:META-INF/spring.factories");for (Resource resource:resources){System.out.println(resource);}//环境信息配置(来源有多个,系统环境变量,properties文件)System.out.println(context.getEnvironment().getProperty("JAVA_HOME"));System.out.println(context.getEnvironment().getProperty("server.port"));//发布事件的方法context.publishEvent(new UserRegisteredEvent(context));}
}

        4.运行结果

http://www.dtcms.com/a/393373.html

相关文章:

  • 【Python】错误和异常
  • 【状态机实现】初识——基于状态机实现的流程编排和Activiti、Camunda、Flowable等工作流的区别
  • SpringBoot自动配置核心原理
  • Python 中的 Builder 模式实践 —— 以 UserProfileBuilder 为例
  • 探秘陌讯AIGC检测算法优化:详解MPS加速与模型热重载的实现原理
  • 1.3 管道(Pipe)核心知识点总结
  • GLUE:自然语言理解评估的黄金基准
  • 第13章 智能监测-设备数据处理
  • GEO技术科普
  • B004基于三菱FX2NPLC智能自提柜控制系统仿真
  • MTK CPU温度调节一知半解
  • V90伺服驱动器“速度模式“双极性模拟量速度控制
  • 课前练习题-20250919
  • C++类与对象
  • 企业级Docker镜像仓库Harbor
  • ESD防护设计宝典(七):生命线的秩序——关键信号线布线规则
  • 【ROS2】Beginner : CLI tools - 理解 ROS 2 话题
  • RL知识回顾
  • Java多线程编程指南
  • 【论文速读】基于地面激光扫描(TLS)和迭 代最近点(ICP)算法的土坝监测变形分析
  • GAMES101:现代计算机图形学入门(Chapter2 向量与线性代数)迅猛式学线性代数学习笔记
  • 汉语构词智慧:从历史优势到现实考量——兼论“汉语全面改造英语”的可能性
  • 仿tcmalloc高并发内存池
  • 墨者学院-通关攻略(持续更新持续改进)
  • 10厘米钢板矫平机:把“波浪”压成“镜面”的科学
  • ESP32- 项目应用1 智能手表之网络配置 #6
  • TCP/IP 互联网的真相:空间域和时间域的统计学
  • 同步与异步
  • C++中char与string的终极对比指南
  • Java基础 9.20