@RepositoryEventHandler事件使用@RepositoryRestController停止 [英] @RepositoryEventHandler events stop with @RepositoryRestController

查看:208
本文介绍了@RepositoryEventHandler事件使用@RepositoryRestController停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为实体创建 @RepositoryRestController 时,在Spring Data中不会触发关联的 @RepositoryEventHandler 方法REST通过Spring Boot 1.4.0.M3(也是Spring Boot 1.3.5) - 这是一个bug,还是设计的

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

@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时触发它们:

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?

如果这是故意的,那很不幸 - 我ll必须实现所有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等事件实施:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆