@RepositoryEventHandler 事件以@RepositoryRestController 停止
@RepositoryEventHandler events stop with @RepositoryRestController
当我为实体创建 @RepositoryRestController
时,关联的 @RepositoryEventHandler
方法不会在 Spring Data REST via Spring Boot 1.4.0.M3 中触发(还有 Spring Boot 1.3.5) -- 这是一个错误,还是 设计的 ?
我有一个 Account
实体 @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);
}
}
当我 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
方法再次触发。
似乎它们应该独立运行,因为它们在 Spring Data REST 中运行两个不同的概念层——或者我误解了什么?
如果这是故意的,那就太不幸了——我将不得不实现所有 HTTP 方法来为具有 @RepositoryRestController
的任何实体自己创建事件。真的是这个意思吗?
已实施。 :-)
@RepositoryRestController
实现中定义的方法替换默认 RepositoryEntityController 中发布 @RepositoryEventHandler
事件的方法。
但是添加这些事件很容易,使 @RepositoryRestControll
成为 ApplicationEventPublisherAware
实现并像默认 RepositoryEntityController
实现一样发布事件:
@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);
}
}
您也可以注入发布者而无需 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);
}
}
当我为实体创建 @RepositoryRestController
时,关联的 @RepositoryEventHandler
方法不会在 Spring Data REST via Spring Boot 1.4.0.M3 中触发(还有 Spring Boot 1.3.5) -- 这是一个错误,还是 设计的 ?
我有一个 Account
实体 @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);
}
}
当我 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
方法再次触发。
似乎它们应该独立运行,因为它们在 Spring Data REST 中运行两个不同的概念层——或者我误解了什么?
如果这是故意的,那就太不幸了——我将不得不实现所有 HTTP 方法来为具有 @RepositoryRestController
的任何实体自己创建事件。真的是这个意思吗?
已实施。 :-)
@RepositoryRestController
实现中定义的方法替换默认 RepositoryEntityController 中发布 @RepositoryEventHandler
事件的方法。
但是添加这些事件很容易,使 @RepositoryRestControll
成为 ApplicationEventPublisherAware
实现并像默认 RepositoryEntityController
实现一样发布事件:
@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);
}
}
您也可以注入发布者而无需 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);
}
}