@RepositoryEventHandler 事件以 @RepositoryRestController 停止 [英] @RepositoryEventHandler events stop with @RepositoryRestController
问题描述
当我为实体创建 @RepositoryRestController
时,关联的 @RepositoryEventHandler
方法不会在 Spring Data REST 中通过 Spring Boot 1.4.0.M3(也是 SpringBoot 1.3.5) -- 这是一个错误,还是设计的?
When I create a @RepositoryRestController
for an entity, the associated @RepositoryEventHandler
methods are not triggered in Spring Data REST via Spring Boot 1.4.0.M3 (also Spring Boot 1.3.5) -- is this a bug, or as designed?
我有一个带有 @RepositoryEventHandler
的 Account
实体:
I have an Account
entity with an @RepositoryEventHandler
:
@Slf4j
@Component
@RepositoryEventHandler(Account.class)
public class AccountEventBridge {
@HandleBeforeCreate
public void handleBeforeCreate(Account account){
log.info("Before create " + account);
}
@HandleAfterCreate
public void handleAfterCreate(Account account){
log.info("Created " + account);
}
}
当我发布时它们应该触发:
which trigger as they should when I POST:
curl -H "Content-Type: application/json" -X POST
-d '{"name":"aaa", "owner":{"email":"aaa@1010","password":"snap"}}'
http://localhost:8080/api/accounts
除非我添加一个 @RepositoryRestController
:
@RepositoryRestController
public class AccountRespositoryRestController {
private final AccountRepository repository;
@Autowired
public AccountRespositoryRestController(AccountRepository repository) {
this.repository = repository;
}
@RequestMapping(method = RequestMethod.POST,value = "/accounts")
public @ResponseBody PersistentEntityResource post(
@RequestBody Account account,
PersistentEntityResourceAssembler assembler) {
// ...
Account entity = this.repository.save(account);
return assembler.toResource(entity);
}
}
当我注释掉 @RepositoryRestController
注释时,@RepositoryEventHandler
方法再次触发.
When I comment out the @RepositoryRestController
annotation, the @RepositoryEventHandler
methods trigger, again.
似乎这些应该独立运行,因为它们在 Spring Data REST 中操作两个不同的概念层——还是我误解了什么?
It seems like these should behave independently since they operate a two different conceptual layers within Spring Data REST -- or am I misunderstanding something?
如果这是故意的,那就太不幸了——我必须自己实现所有 HTTP 方法来为具有 @RepositoryRestController
的任何实体创建事件.真的是这个意图吗?
If this is intentional, it's unfortunate -- I'll have to implement all HTTP methods to create the events myself for any entity with an @RepositoryRestController
. Is that really the intent?
推荐答案
已实现.:-)
@RepositoryRestController
实现中定义的方法替换默认 RepositoryEntityController 发布 @RepositoryEventHandler
事件.
The methods defined in a @RepositoryRestController
implementation replace the methods in the default RepositoryEntityController which publish @RepositoryEventHandler
events.
但是很容易添加这些事件,使 @RepositoryRestControll
成为 ApplicationEventPublisherAware
实现并像默认的 RepositoryEntityController
实现一样发布事件:p>
But it's easy to add these events making the @RepositoryRestControll
a ApplicationEventPublisherAware
implementation and publishing the events like the default RepositoryEntityController
implementation:
@Slf4j
@RepositoryRestController
@AllArgConstructor
public 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
的情况下注入发布者:
You can also inject the publisher without making the class ApplicationEventPublisherAware
:
@Slf4j
@RepositoryRestController
@AllArgConstructor
public 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);
}
}
这篇关于@RepositoryEventHandler 事件以 @RepositoryRestController 停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!