Jan*_*sen 2 java-8 spring-data spring-data-rest spring-boot
当我@RepositoryRestController为一个实体创建一个实体时,相关的@RepositoryEventHandler方法不是通过Spring Boot 1.4.0.M3(也是Spring Boot 1.3.5)在Spring Data REST中触发的 - 这是一个bug,还是设计的?
我有一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
在POST时它会触发它们:
curl -H "Content-Type: application/json" -X POST
-d '{"name":"aaa", "owner":{"email":"aaa@1010","password":"snap"}}'
http://localhost:8080/api/accounts
Run Code Online (Sandbox Code Playgroud)
除非我添加@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);
}
}
Run Code Online (Sandbox Code Playgroud)
当我注释掉@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);
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以在不进行课程的情况下注入发布者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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
977 次 |
| 最近记录: |