它 已实现 。:-)
在
@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); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)