1. Introduction
1.绪论
While working with an entity, the REST exporter handles operations for creating, saving, and deleting events. We can use an ApplicationListener to listen to these events and execute a function when the particular action is performed.
在处理实体时,REST输出器会处理创建、保存和删除事件的操作。我们可以使用ApplicationListener来监听这些事件,并在执行特定操作时执行一个函数。
Alternatively, we can use annotated handler which filters events based on domain type.
另外,我们可以使用注释处理程序,根据领域类型过滤事件。
2. Writing an Annotated Handler
2.编写一个注释处理程序
The ApplicationListener doesn’t distinguish between entity types; but with the annotated handler, we can filter events based on domain type.
ApplicationListener并不区分实体类型;但是通过注释的处理程序,我们可以根据领域类型过滤事件。
We can declare an annotation based event handler by adding @RepositoryEventHandler annotation on a POJO. As a result, this informs the BeanPostProcessor that the POJO needs to be inspected for handler methods.
我们可以通过在POJO上添加@RepositoryEventHandler注解来声明一个基于注解的事件处理器。因此,这将通知BeanPostProcessor该POJO需要被检查以获得处理方法。
In the example below, we annotate the class with RepositoryEventHandler corresponding to the entity Author – and declare methods pertaining to different before and after events corresponding to the Author entity in the AuthorEventHandler class:
在下面的例子中,我们用RepositoryEventHandler对应实体Author – 来注释该类,并在AuthorEventHandler类中声明与Author实体对应的不同前后事件有关的方法。
@RepositoryEventHandler(Author.class)
public class AuthorEventHandler {
Logger logger = Logger.getLogger("Class AuthorEventHandler");
@HandleBeforeCreate
public void handleAuthorBeforeCreate(Author author){
logger.info("Inside Author Before Create....");
String name = author.getName();
}
@HandleAfterCreate
public void handleAuthorAfterCreate(Author author){
logger.info("Inside Author After Create ....");
String name = author.getName();
}
@HandleBeforeDelete
public void handleAuthorBeforeDelete(Author author){
logger.info("Inside Author Before Delete ....");
String name = author.getName();
}
@HandleAfterDelete
public void handleAuthorAfterDelete(Author author){
logger.info("Inside Author After Delete ....");
String name = author.getName();
}
}
Here, different methods of the AuthorEventHandler class will be invoked based on the operation performed on Author entity.
在这里,AuthorEventHandler类的不同方法将根据对Author实体进行的操作而被调用。
On finding the class with @RepositoryEventHandler annotation, Spring iterates over the methods in the class to find annotations corresponding to the before and after events mentioned below:
在找到带有@RepositoryEventHandler注解的类时,Spring会遍历该类中的方法,找到与下面提到的前后事件相对应的注解。
Before* Event Annotations – associated with before annotations are called before the event is called.
Before*事件注解–与before注解相关联,在事件被调用之前被调用。
- BeforeCreateEvent
- BeforeDeleteEvent
- BeforeSaveEvent
- BeforeLinkSaveEvent
After* Event Annotations – associated with after annotations are called after the event is called.
After*事件注解–与after注解相关的是在事件被调用后被调用。
- AfterLinkSaveEvent
- AfterSaveEvent
- AfterCreateEvent
- AfterDeleteEvent
We can also declare methods with different entity type corresponding to the same event type in a class:
我们也可以在一个类中用不同的实体类型来声明对应同一事件类型的方法。
@RepositoryEventHandler
public class BookEventHandler {
@HandleBeforeCreate
public void handleBookBeforeCreate(Book book){
// code for before create book event
}
@HandleBeforeCreate
public void handleAuthorBeforeCreate(Author author){
// code for before create author event
}
}
Here, the BookEventHandler class deals with more than one entity. On finding the class with @RepositoryEventHandler annotation, it iterates over the methods and calls the respective entity before the respective create event.
在这里,BookEventHandler 类处理了一个以上的实体。在找到带有@RepositoryEventHandler 注解的类时,它将遍历这些方法并在各自的创建事件之前调用相应的实体。
Also, we need to declare the event handlers in the @Configuration class which will inspect the bean for handlers and matches them with the right events:
另外,我们需要在@Configuration类中声明事件处理程序,该类将检查Bean的处理程序并将其与正确的事件相匹配。
@Configuration
public class RepositoryConfiguration{
public RepositoryConfiguration(){
super();
}
@Bean
AuthorEventHandler authorEventHandler() {
return new AuthorEventHandler();
}
@Bean
BookEventHandler bookEventHandler(){
return new BookEventHandler();
}
}
3. Conclusion
3.总结
In conclusion, this serves as an introduction to implementing and understanding @RepositoryEventHandler.
总之,这可以作为实现和理解@RepositoryEventHandler的介绍。
In this quick tutorial, we learned how to implement @RepositoryEventHandler annotation to handle various events corresponding to entity type.
在这个快速教程中,我们学习了如何实现@RepositoryEventHandler注解来处理实体类型对应的各种事件。
And, as always, find the complete code samples over on Github.
而且,像往常一样,在Github上找到完整的代码样本。