Spring事件框架

Spring框架 ApplicationContext 事件监听 ApplicationContext的事件 ApplicationContext app; ApplicationContext 设置对应的配置文件用的 事件的监听。初期,开始,停止,终了 eg 在创建自定义监听类的时候。需要继承ApplicationListener - MyApplicationListener.java package com.luffy.code; import or... 阅读详情

1.定义事件类继承ApplicationEvent抽象类:

import org.springframework.context.ApplicationEvent;

public class RegisterEvent extends ApplicationEvent {

	private static final long serialVersionUID = 1L;
	String userName;

	public RegisterEvent(Object source, String msg) {
		super(source);
		this.userName = msg;
	}

	public String getUserName() {
		return userName;
	}

}

2.定义监听类实现ApplicationListener接口:

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

@Service
public class RegisterAccountSendMailListener implements
		ApplicationListener<RegisterEvent> {

	@Override
	public void onApplicationEvent(RegisterEvent arg0) {
		System.out.println("发邮件到:" + arg0.userName);

	}

}


3.测试类:

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

public class RegisterServices {

	private ApplicationContext context;

	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		this.context = arg0;
	}

	public void register() {
		String username = "大大雄";
		System.out.println("用户名可以用!");
		// do something...
		System.out.println("用户注册成功!");
		context.publishEvent(new RegisterEvent(this, username));
	}

	public static void main(String[] args) {
		RegisterServices registerServices = new RegisterServices();
		registerServices
				.setApplicationContext(new ClassPathXmlApplicationContext(
						"springEvent/applicationContext.xml"));
		registerServices.register();
	}

}

加载spring配置文件的时候,Spring框架会将所有的listener都加载出来,并把它们放在一个集合中。执行context.publishEvent(new RegisterEvent(this, username));方法是会通过反射找到所有监听了RegisterEvent类的listener,通过循环执行listener.onApplicationEvent(event);方法
带你吃透 Spring 框架中的事件处理机制 在现代应用程序中,各个组件之间的通信是至关重要的。想象一下,你的应用程序中的各个模块像是一个巨大的交响乐团,每个模块都是一位音乐家,而Spring事件机制就像是指挥家,将所有音乐家协调得天衣无缝。这种松耦合的通信方式使你的应用程序更加灵活、可维护,而且能够轻松应对变化。现在,让我们进入这个令人兴奋的音乐厅,探索Spring事件的世界。在Spring框架中,事件(Events)是一种基本概念,用于实现松耦合的通信方式,允许不同组件之间进行相互通知和协作。 阅读详情

相关推荐

Java Spring 框架的事件驱动机制

本文旨在全面解析Spring框架中的事件驱动机制,帮助开发者理解其内部工作原理并掌握实际应用技巧。内容涵盖从基础概念到高级特性,包括同步/异步事件处理、事务绑定事件、条件化监听等核心功能。文章首先介绍Spring事件机制的基本概念,然后深入分析其实现原理,接着通过实际案例展示各种应用场景,最后讨论性能优化和扩展方案。: Spring框架中所有事件的基类: 监听并处理特定类型事件的接口: 负责发布事件的核心接口: 事件多播器,负责将事件分发给合适的监听器响应式编程集成。

Java大师兄的博客 1153

观察者模式Spring之publishEvent事件处理

一、使用场景 这个一般什么时候使用,我们一般是在不同的bean直接进行信息传递,比如我们beanA的事件处理完后,需要beanB进行处理一些业务逻辑的时候这种情况就一般可以使用publish-event解决。 二、原理 ApplicationContext中的事件处理是通过ApplicationEvent类和ApplicationListener接口来提供的,通过ApplicationContext的publishEvent()方法发布到ApplicationListener;。 一个事件模型有三个组成部分

jike11231的博客 1万+

Spring中ApplicationContext对事件传递

Spring中ApplicationContext对事件传递

Spring中的ApplicationContext.publishEvent

spring中事件监听的使用方法,applicationContext.publishEvent的使用

qq_44681872的博客 3295

论文笔记:Group Equivariant Convolutional Networks

Group Equivariant Convolutional Networks 1、四个问题 要解决什么问题? 对卷积神经网络进行扩展,并提出一个在特定的变换(旋转、平移等,也可表示为一个特殊的群)下具有等变性的网络。 用了什么方法解决? 提出了一种新的卷积神经网络结构——群等变卷积神经网络(Group equivariant Convolutional Neural Network)...

hongbin_xu的博客 1万+

Windows事件ID 10010报错:DCOM注册超时与Game Bar修复指南

DCOM(分布式组件对象模型)是Windows中用于跨进程组件通信的核心机制,其注册超时问题常表现为事件ID 10010报错。当Game Bar的PresenceWriter组件因权限配置不当、组件损坏或开机资源竞争而无法在限定时间内向DCOM注册时,系统会记录该错误,并可能伴随DCOM服务主机CPU占用升高或游戏栏功能异常。理解DCOM激活原理与Network Service账户权限,有助于从权限修复、组件重注册及开机时序优化等层面系统排查。本文针对不同Windows版本,提供从静默处理到根治的完整方案,

weixin_33563728的博客 981

Spring观察者模式--publishEvent事件发布处理

spring观察者模式实操

打怪升级 3428

信号编辑软件ZX/SignalEditing

信号编辑软件ZX/SignalEditing

weixin_45661395的博客 969

ApplicationContext.publishEvent()-异步调用

/继承 ApplicationEvent public class EventDemo extends ApplicationEvent {} }//继承 ApplicationEvent public class EventDemo extends ApplicationEvent {} }

qq_45213212的博客 2389

电力远动入门:手把手解析IEC 60870-5-101规约的帧结构与报文交互(基于DL/T634.5101-2002)

本文深入解析IEC 60870-5-101规约的帧结构与报文交互,适用于电力远动通信系统。通过详细讲解规约的基础架构、控制域位定义及典型报文交互流程,帮助工程师快速掌握101规约的核心技术,提升电力自动化系统的调试与维护能力。

weixin_27076351的博客 249

西工大noj100题python2024第三季21-30

【代码】西工大noj100题python2024第三季21-30。

weixin_70523540的博客 1580

ApplicationListener使用和发布原理

目录1. 演示2. 实现原理2.1 ContextRefreshedEvent大概流程2.2 applicationContext.publishEvent()大概流程2.3 ContextClosedEvent大概流程2.4 细节一:如何获取派发器2.5 细节二:容器如何获取所有监听器2.6 总结流程 记录最近看视频学到的AppiicationListener的原理 ApplicationListener是用于容器初始化之后调用,如果想要在IOC容器创建完bean之后或者容器关闭的时候做一些操作,就可以用

Jwasx的博客 1181

数字电路逻辑与设计实验一 门电路逻辑功能及测试

(1)选二输入四异或门电路 74LS86,按图 1-8 接线,输入端 1、2、4、5 接逻辑开 关(S1~S4),输出端 A、B、Y 接电平显示发光二极管.用六反相器(非门)按图 1-11 接线,输入 80KHz 连续脉冲,用双踪示波器测输入, 输出相位差,计算每个门的平均传输延迟时间的 tpd 值。测试不同的输入组合:对门电路进行全面的测试,尝试不同的输入组合,包括各种可能的逻辑状态。检查门电路的输入:确认门电路的输入是否正确,并且输入信号的电平符合门电路的规格要求。确保输入信号的稳定性和准确性。

欢迎交流学习心得 2万+

Spring 框架中有哪些不同类型的事件?

Spring 框架中有哪些不同类型的事件? Spring 提供了以下5 种标准的事件: (1)上下文更新事件(ContextRefreshedEvent):在调用 ConfigurableApplicationContext 接口中的refresh()方法时被触发。 (2)上下文开始事件(ContextStartedEvent):当容器调用 ConfigurableApplicationContext 的Start()方法开始/重新开始容器时触发该事件。 (3)上下文停止事件(ContextStoppedE

LiJianGuo_Mr的博客 489

14、PostgreSQL日志文件、系统统计与查询优化全解析

本文全面解析了PostgreSQL数据库中的系统统计、日志文件配置与查询优化技巧。详细介绍了如何利用pg_stat_statements模块进行性能监控,配置日志记录以发现潜在问题,并深入探讨了查询优化器的工作原理和优化策略。通过实际案例分析,展示了如何综合应用这些技术提升数据库性能。适合希望提升PostgreSQL性能与稳定性的数据库管理员和开发人员阅读。

efc123456的博客 94

使用Matlab simulink的PLC coder功能进行算法搭建与处理

使用的案例如下: Anti-Windup PID Control Demonstration with Feedforward Control 将其中的PID块生成SCL语言再导入TIA 使用的平台如下: Matlab 2019b TIA portal 15.1 应用:可以在PLC中搭载一些并不方便使用的,或者比较难使用的算法 步骤如下: 首先打开案例,案例地址我放在最下面,点击Run运行仿真,必...

苦走一口田 1万+

事件的力量:探索Spring框架中的事件处理机制

了解如何使用Spring的事件处理机制来实现异步通信、解耦组件,以及提高应用程序的可扩展性。本文将带你深入事件的世界。

todoitbo的博客 5368

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

全面探索Spring框架中的事件处理机制

在现代应用程序中,各个组件之间的通信是至关重要的。想象一下,你的应用程序中的各个模块像是一个巨大的交响乐团,每个模块都是一位音乐家,而Spring事件机制就像是指挥家,将所有音乐家协调得天衣无缝。这种松耦合的通信方式使你的应用程序更加灵活、可维护,而且能够轻松应对变化。现在,让我们进入这个令人兴奋的音乐厅,探索Spring事件的世界。在Spring框架中,事件(Events)是一种基本概念,用于实现松耦合的通信方式,允许不同组件之间进行相互通知和协作。

专注Java后端技术干货、项目源码总结分享,期待您的关注。 1149

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Spring框架中有哪些不同类型的事件

欢迎大家加入我的开源项目,或者(在我的主页联系我)加入你们的开源项目,点点Github-Stars。

jj89929665的博客 1087

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Spring框架中事件发布及其原理

Spring框架中事件发布及其原理事件源发布事件定义事件源定义事件定义监听器测试springContextHolderApplicationContextAware接口ApplicationContext发布事件的原理AbstractApplicationContext事件广播earlyApplicationEventsApplicationEventMulticasterSimpleApplicationEventMulticaster总结附录 在业务系统中,当多个模块之间需要进行通信的时候,事件可以作

Mr_YuanD的博客 2125

一文读懂Spring框架中的不同类型事件

本文介绍Spring框架中的不同类型事件及其相关概念。这些事件包括应用事件、上下文事件、有序事件和异步事件,每种事件都有其独特用途和特点。文章还展示如何创建自定义事件和应用事件监听器,并解释如何使用这些事件和监听器以实现组件之间的松耦合通信。

孤芳不自赏 422

Spring框架中的事件机制:深入理解与实践

Spring框架是一个广泛使用的Java企业级应用框架,提供了依赖注入、面向切面编程(AOP)、事务管理、Web应用程序开发等一系列功能。在Spring框架中,事件机制是一种重要的通信方式,它允许不同组件之间进行松耦合的通信,提高了应用程序的可维护性和可扩展性。本文将深入探讨Spring框架中的事件机制,包括不同类型的事件、底层原理、应用实践以及优缺点。以下是一个简单的示例,演示了如何在Spring框架中定义和使用自定义事件与监听器。首先,定义一个自定义事件类,继承自。

喜欢猪猪 1004

Spring框架事件监听使用方式

在本文中,我们介绍了如何在 Spring 框架中使用事件监听。我们创建了一个自定义事件,实现了一个事件监听器,并使用发布事件。这种机制允许我们在应用程序中处理各种事件,提高了代码的可扩展性和可维护性。

weixin_43107388的博客 349

Spring框架中的事件处理机制

需要创建一个继承自的自定义事件类,并定义事件的数据。首先,需要创建一个继承自org.springframework.context.ApplicationEvent的自定义事件类。这个类将包含希望在应用程序中传递的事件信息。可以在自定义事件类中添加任何属性和方法,以满足需求。例如,假设要创建一个名为在上述示例中,类包含了一个username属性,用于存储注册用户的用户名。要发布自定义事件,需要获取对象并使用它来发布事件。这通常通过在需要发布事件的类中注入来实现。@Service。

凤凰的博客 890

经典面试题-Spring框架中有哪些不同类型的事件?

Spring的ApplicationContext 提供了支持事件和代码中监听器的功能。 我们可以创建bean用来监听在ApplicationContext 中发布的事件。ApplicationEvent类和在ApplicationContext接口中处理的事件,如果一个bean实现了ApplicationListener接口,当一个ApplicationEvent 被发布以后,bean会自动被通...

逍遥云恋 2754

Spring 源码学习 19:事件框架

本文介绍了Spring事件框架的使用及其实现原理。主要内容包括:1)利用事件机制解耦业务逻辑(如发送邮件/短信)与主业务代码,通过定义事件类、发布事件和创建监听器实现;2)使用@EventListener注解简化监听器实现;3)配置多线程事件处理,通过自定义ApplicationEventMulticaster实现;4)深入分析@EventListener注解原理,通过自定义注解并利用SmartInitializingSingleton接口动态注册监听器。文章通过代码示例展示了Spring事件的灵活应用方式

梦幻天空 532
上一篇: SpringMVC中的小知识
下一篇: UML类图关系大全
Notebookdong
博客等级 码龄15年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值