如何在 crud 事件之前或之后执行代码 [英] How to execute code before or after a crud event
问题描述
我正在尝试学习 Java Spring 框架.我一直在关注一些书籍和指南,但它们都是标准的 ToDo(HelloWorld)类型的应用程序.
I am trying to learn Java Spring framework. I have been following some books and guides however they are all the standard ToDo ( HelloWorld ) type applications.
在我一直遵循的指南中通常未涉及的一个主题是如何在执行完全不同(但通常相关)记录的 crud 操作时更新其他系统记录.
One topic that has commonly been untouched in the guides I have been following is how to update other system records upon the execution of crud operations of an entirely different (however usually related) record.
这是一个人为的例子.
我有一个对象 ToDo
保存在数据库中.我还有另一个对象 ToDoDailyReport
,它保存在数据库中(忽略这可能是即时计算的事实).
I have an object ToDo
which is persisted in the database. I also have another object ToDoDailyReport
which is persisted in the database ( ignore the fact that this could probably be calculated on the fly ).
如果我对 ToDo
执行任何 crud 操作,我想更新 ToDoDailyReport
If I execute any crud operation on a ToDo
I would like to update the ToDoDailyReport
我正在使用 spring-boot-starter-data-rest
并创建 CrudRepositories
用 @RepositoryRestResource
注释它们,类似于这个例子( 这里 )
I am using spring-boot-starter-data-rest
and creating CrudRepositories
annotating them with @RepositoryRestResource
similar to this example ( Here )
在我看来,我想我可以使用 @RestController
创建一堆自定义控制器并实现我想要的任何行为,但是我喜欢保留它们的 CrudRepositories 原样并简单地在之前或之前执行一个函数的想法在 crud 操作被触发后.我看过关于 AOP
和 AspectJ
的讨论,但是我不确定这些方法是否适用于我的用例,或者我应该实施什么方法以及我将如何触发 AOP 建议
在 crud 操作之前或之后.
In my mind I imagine I could create a bunch of custom controllers using @RestController
and implement whatever behaviour I want however I like the idea of leaving their CrudRepositories as is and simply executing a function before or after the crud operation is fired. I have seen discussion of AOP
and AspectJ
however I am not sure if these approaches are meant for my use case or what approach I should be implementing and how I would fire a AOP advice
before or after the crud operation.
任何建议或方向将不胜感激.
Any advice or direction would be appreciated.
推荐答案
我是 Spring Boot 的新手,所以这是否是解决这个问题的正确方法还有待观察,但问题是
I am new to Spring Boot, so whether or not this is the correct approach to solve this problem will remain to be seen however the question
如何在 crud 事件之前或之后执行代码"
"How to execute code before or after a crud event"
使用在 CRUD 操作期间由 REST 导出器触发的事件来回答.
Is answered by using events which are fired by the REST exporter during CRUD operations.
这篇关于如何在 crud 事件之前或之后执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!