gpt4 book ai didi

spring-boot - @RepositoryEventHandler 事件以 @RepositoryRestController 停止

转载 作者:行者123 更新时间:2023-12-01 08:52:48 25 4
gpt4 key购买 nike

当我创建 @RepositoryRestController对于实体,关联的 @RepositoryEventHandler方法不是通过 Spring Boot 1.4.0.M3(也是 Spring Boot 1.3.5)在 Spring Data REST 中触发的——这是一个错误,还是设计的?

我有一个 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);
}
}

当我发布时它们应该触发:
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);
}
}

您也可以在不创建类 ApplicationEventPublisherAware 的情况下注入(inject)发布者:
@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);
}
}

关于spring-boot - @RepositoryEventHandler 事件以 @RepositoryRestController 停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37603231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com