http://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2

Better application events in Spring Framework 4.2

 

FEBRUARY 11, 2015

Application events are available since the very beginning of the Spring framework as a mean for loosely coupled components to exchange information. One of the most well known usage of application events is the following:

@Component
public class MyListener
implements ApplicationListener<ContextRefreshedEvent> { public void onApplicationEvent(ContextRefreshedEvent event) {
...
}
}

This allows MyListener to be notified when the context has refreshed and one can use that to run arbitrary code when the application context has fully started.

In Spring Framework 4.2 we have revisited the event infrastructure in three main areas that I am going to explain in this post.

Generics support

It is now possible to define your ApplicationListener implementation with nested generics information in the event type, something like:

public class MyListener
implements ApplicationListener<MyEvent<Order>> { ... }

When dispatching an event, the signature of your listener is used to determine if it matches said incoming event.

Due to type erasure you need to publish an event that resolves the generics parameter you would filter on, something like MyOrderEvent extends MyEvent<Order>. There might be other workarounds and we are happy to revisit the signature matching algorithm if the community thinks it worthwhile.

Annotation-driven event listener

The biggest new feature is the support of annotation-driven event listeners, similar to our recent work on JMS and AMQP endpoints in Spring Framework 4.1. In a nutshell, it is now possible to simply annotate a method of a managed-bean with @EventListener to automatically register an ApplicationListener matching the signature of the method. Our example above can be rewritten as follows:

@Component
public class MyListener { @EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
...
}
}

@EventListener is a core annotation that is handled transparently in a similar fashion as@Autowired and others: no extra configuration is necessary with java config and the existing<context:annotation-driven/> element enables full support for it.

The method signature defines the event type that you’re interested in. It is also possible to define a SpEL expression that should match in order to handle the event. For instance, consider the following event:

public class OrderCreatedEvent implements CreationEvent<Order> { ... }

    private boolean awesome;

    public boolean isAwesome() { return this.awesome; }
....
}

The following example showcases an event listener that will only be invoked for an awesomeCreationEvent of Order (i.e. if the awesome flag is true):

@Component
public class MyComponent { @EventListener(condition = "#creationEvent.awesome")
public void handleOrderCreatedEvent(CreationEvent<Order> creationEvent) {
...
} }

As you can see from the sample above, method arguments are exposed via their names if such information can be discovered. The condition expression also exposes a “root” variable with the raw ApplicationEvent (#root.event) and the actual method arguments (#root.args).

Publishing events

You can define a non-void return type for any method annotated with @EventListener. If you return a non null value as the result of the processing of a particular event, we’ll send that result as a new event for you.

You may have noticed that our OrderCreatedEvent does not extend fromApplicationEvent; we felt it was about time to give you the flexibility to publish any arbitrary event and not force you to extend from ApplicationEvent. TheApplicationEventPublisher interface has been extended to allow you to publish any object; when said object isn’t an ApplicationEvent, we wrap it in a PayloadApplicationEvent for you. Remember this if you want to listen to such arbitrary event using a regularApplicationListener implementation.

The following sample shows how you can use ApplicationEventPublisher to send anOrderCreatedEvent:

@Component
public class MyComponent { private final ApplicationEventPublisher publisher; @Autowired
public MyComponent(ApplicationEventPublisher publisher) { ... } public void createOrder(Order order) {
// ....
this.publisher.publishEvent(new OrderCreatedEvent(order));
} }

Transaction bound events

Another popular improvement is the ability to bind the listener of an event to a phase of the transaction. The typical example is to handle the event when the transaction has completed successfully: this allows events to be used with more flexibility when the outcome of the current transaction actually matters to the listener.

Spring Framework is currently structured in such a way that the context is not aware of the transaction support and we obviously didn’t want to deviate from that very sane principle so we built an open infrastructure to allow additional components to be registered and influence the way event listeners are created.

The transaction module implements an EventListenerFactory that looks for the new@TransactionalEventListener annotation. When this one is present, an extended event listener that is aware of the transaction is registered instead of the default.

Let’s reuse our example above and rewrite it in such a way that the order creation event will only be processed when the transaction in which the producer is running has completed successfully:

@Component
public class MyComponent { @TransactionalEventListener(condition = "#creationEvent.awesome")
public void handleOrderCreatedEvent(CreationEvent<Order> creationEvent) {
...
} }

Not much to see, right? @TransactionalEventListener is a regular @EventListener and also exposes a TransactionPhase, the default being AFTER_COMMIT. You can also hook other phases of the transaction (BEFORE_COMMITAFTER_ROLLBACK and AFTER_COMPLETIONthat is just an alias for AFTER_COMMIT and AFTER_ROLLBACK).

By default, if no transaction is running the event isn’t sent at all as we can’t obviously honor the requested phase, but there is a fallbackExecution attribute in@TransactionalEventListener that tells Spring to invoke the listener immediately if there is no transaction.

Try it out!

If you want to give this a try before the first milestone release of 4.2, grab a nightly SNAPSHOT build via our snapshot repository. You can also create a sample project usingstart.spring.io using the latest Spring Boot snapshot build, or if you’re super lazy you can copy/paste this in your shell:

$ curl https://start.spring.io/starter.tgz -d artifactId=events-demo \
-d baseDir=events-demo -d bootVersion=1.2.2.BUILD-SNAPSHOT | tar -xzvf -

And update the project to use Spring Framework 4.2.0.BUILD-SNAPSHOT

<properties>
...
<spring.version>4.2.0.BUILD-SNAPSHOT</spring.version>
</properties>

As always, we welcome community feedback, please try these features and let us know if you run into any issue.

Spring 4.2 annotation event Publisher/Listener的更多相关文章

  1. [key]严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener(Spring配置异常)

    详细错误为: 严重: Exception sending context initialized event to listener instance of class org.springframe ...

  2. Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  3. 严重: Exception sending context initialized event to listener instance of class org.springframework.we

    2014-6-1 0:47:25 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat ...

  4. Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException:

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  5. spring定时器用Annotation兑现

    spring定时器用Annotation实现 0人收藏此文章, 我要收藏发表于3个月前 , 已有46次阅读 共0个评论 1.ApplicationContext.xml配置 a).需要在xmlns里面 ...

  6. Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener. ...nested exception is java.lang.NoSuchMethodError:

    ssh 中,项目部署到服务器的时候,出现这样的奇葩的事情: 21-Oct-2017 11:27:15.953 INFO [localhost-startStop-1] org.apache.catal ...

  7. [置顶] struts2+hibernate+spring整合(annotation版)

    本博文使用struts2,hibernate,spring技术整合Web项目,同时分层封装代码,包含model层,DAO层,Service层,Action层. 在整合hibernate时使用annot ...

  8. 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

    十月 30, 2019 11:12:35 下午 org.apache.catalina.core.StandardContext listenerStart 严重: Exception sending ...

  9. 严重: Exception sending context initialized event to listener instance of class

    问题描述:Exception sending context initialized event to listener instance of class org.springframework.w ...

随机推荐

  1. Oracle数据库安装后,登录Database Control时密码错误

    解决方案1(实测可行): sys 和 system用户的用户名和密码还记得不?试试看如果不行,用sqlplus 在服务器本地登录%sqlplus / as sysdbaSQL>alter use ...

  2. IE兼容性问题解决方案4--form表单在IE下重复提交

    遇到过一种情况,点击提交按钮的时候,在IE下重复提交,而在其他浏览器下正常. 原因:button按钮不设置type时,在IE下被浏览器默认解析为type="submit",用js提 ...

  3. wuzhicms 模块开发

    首先,模块开发需要了解五指cms的目录结构: 然后,我们需要新增加一个模块目录: 再app下面创建 如:content 下面包含文件: 前台文件的创建: 看下 index.php 的内容: <? ...

  4. wuzhicms私密下载链接生成

    加载函数库:load_function('content','content'); echo private_file('http://dev.wuzhicms.com/uploadfile/2014 ...

  5. Node与Express开发 坑1

    添加 app.set('views', __dirname + '/views') 修改 app.use(express.static(__dirname + '/public')); express ...

  6. RxJava 复杂场景 Schedulers调度

    参考: https://blog.piasy.com/2016/10/14/Complex-RxJava-2-scheduler/ 以Zip为例,学习Schedulers的线程调度 要求: * cre ...

  7. 【spoj SEQN】【hdu 3439】Sequence

    题意: 给出n.m.k 求C(n,k)*H(n-k)%m的值 H(n-k)为错排公式 题解: 先算H(n-k) 计算H(n)有个通式: H(n)=(-1)^n+((-1)^(n-1))n+((-1)^ ...

  8. HDU-3487 Play with Chain Splay tee区间反转,移动

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 对于一个数列有两种操作:1.CUT a b c,先取出a-b区间的数,然后把它们放在取出后的第c ...

  9. Java 远程通讯技术及原理分析

    在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如:RMI.MINA.ESB.Burlap.Hessian.SOAP.EJB和JMS等,这些 ...

  10. 家庭局域网接入Internet

     接入Internet  建立宽带连接 步骤一:单击"网络",再属性 步骤二:单击"更改适配器设置" 步骤三:选择"宽带连接" 步骤四:输入 ...