美文网首页
JAVA调用MongoDB生命周期事件 - MongoDB从入门

JAVA调用MongoDB生命周期事件 - MongoDB从入门

作者: DreamsonMa | 来源:发表于2019-01-31 16:15 被阅读0次

监听MongoDB的生命周期,只需重写org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener的子类,并在SpringApplicationContext中声明这些bean,这样在分派事件时会调用它们。例如:

public class BeforeConvertListener extends AbstractMongoEventListener<Person> {
  @Override
  public void onBeforeConvert(BeforeConvertEvent<Person> event) {
    ... does some auditing manipulation, set timestamps, whatever ...
  }
}

AbstractMappingEventListener中提供了以下回调方法:

方法 描述
onBeforeConvert 调用MongoTemplateinsertinsertListsave操作,在通过MongoConverter将对象转换为文档之前的处理。
onBeforeSave 调用MongoTemplateinsertinsertListsave操作,在数据库中插入或保存文档之前的处理。
onAfterSave 调用MongoTemplateinsertinsertListsave操作,在数据库中插入或保存文档之后的处理。
onAfterLoad 调用MongoTemplate中的findfindAndRemovefindOnegetCollection方法,从数据库检索文档后的处理。
onAfterConvert 调用MongoTemplate中的findfindAndRemovefindOnegetCollection方法,从数据库检索文档被转换为POJO后的处理。

相关文章

网友评论

      本文标题:JAVA调用MongoDB生命周期事件 - MongoDB从入门

      本文链接:https://www.haomeiwen.com/subject/ivtlsqtx.html