@RepositoryEventHandler事件以@RepositoryRestController停止

@RepositoryEventHandler事件以@RepositoryRestController停止,第1张

@RepositoryEventHandler事件以@RepositoryRestController停止

已实现 。:-)

@RepositoryRestController
实现中定义的方法替换了发布事件的默认RepositoryEntityController中的方法
@RepositoryEventHandler

但是添加这些事件使其

@RepositoryRestControll
成为
ApplicationEventPublisherAware
实现并像默认
RepositoryEntityController
实现一样发布事件很容易:

@Slf4j@RepositoryRestController@AllArgConstructorpublic class AccountRespositoryRestController     implements ApplicationEventPublisherAware {    private final AccountRepository repository;    private ApplicationEventPublisher publisher;    @Override    public void setApplicationEventPublisher(        ApplicationEventPublisher publisher) {        this.publisher = publisher;    }    @RequestMapping(method = RequestMethod.POST,value = "/accounts")    public @ResponseBody PersistentEntityResource post(        @RequestBody Account account,        PersistentEntityResourceAssembler assembler) {        // ...        publisher.publishEvent(new BeforeCreateEvent(account));        Account entity = this.repository.save(account);        publisher.publishEvent(new AfterCreateEvent(entity));        return assembler.toResource(entity);    }}

您也可以在不上课的情况下注入发布者

ApplicationEventPublisherAware

@Slf4j@RepositoryRestController@AllArgConstructorpublic class AccountRespositoryRestController {    private final AccountRepository repository;    private final ApplicationEventPublisher publisher;    @RequestMapping(method = RequestMethod.POST,value = "/accounts")    public @ResponseBody PersistentEntityResource post(        @RequestBody Account account,        PersistentEntityResourceAssembler assembler) {        // ...        publisher.publishEvent(new BeforeCreateEvent(account));        Account entity = this.repository.save(account);        publisher.publishEvent(new AfterCreateEvent(entity));        return assembler.toResource(entity);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4885218.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-11
下一篇 2022-11-11

发表评论

登录后才能评论

评论列表(0条)

保存