Java 拦截对MongoDB Spring Boot的所有写入调用
我正在使用Java 拦截对MongoDB Spring Boot的所有写入调用,java,spring-boot,spring-data-mongodb,Java,Spring Boot,Spring Data Mongodb,我正在使用Spring Boot 2.1.5。我正在尝试编写一个服务来拦截对MongoDB的所有写调用。基本上类似于DBMS中的操作日志,在任何数据写入DB或更新或删除之前,我捕获正在更新或创建的文档 这可能吗?如果是,怎么做 它用于从我的应用程序中进行的DB调用。因为您看到,假设您有一个包含100个左右API的大型应用程序,所以很难在所有API控制器或服务中实际集成OpLog(DB更新、写入、删除),而不是编写一个在调用MongoRepository或MongoTemplate函数之前触发的拦
Spring Boot 2.1.5
。我正在尝试编写一个服务来拦截对MongoDB的所有写调用。基本上类似于DBMS中的操作日志,在任何数据写入DB或更新或删除之前,我捕获正在更新或创建的文档
这可能吗?如果是,怎么做
它用于从我的应用程序中进行的DB调用。因为您看到,假设您有一个包含100个左右API的大型应用程序,所以很难在所有API控制器或服务中实际集成OpLog(DB更新、写入、删除),而不是编写一个在调用MongoRepository或MongoTemplate函数之前触发的拦截器或方面
您可以扩展org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener并实现自定义侦听器 与spring文档有关吗 AbstractMappingEventListener中存在以下回调方法: onBeforeConvert:在MongoTransfer将对象转换为文档之前,在MongoTemplate插入、插入列表和保存操作中调用 onBeforeSave:在MongoTemplate insert、insertList和save操作中调用,然后在数据库中插入或保存文档 onAfterSave:在MongoTemplate中调用insert、insertList和save操作,在数据库中插入或保存文档后调用 onAfterLoad:从数据库检索文档后,在MongoTemplate find、findAndRemove、findOne和getCollection方法中调用 onAfterConvert:从数据库检索文档并将其转换为POJO后,在MongoTemplate find、findAndRemove、findOne和getCollection方法中调用 如果要记录所有操作,可以定义org.springframework.data.mongodb.core.mapping.event.LoggingEventListener的bean
@Configuration
public class MongoConfig {
@Bean
public LoggingEventListener<Object> listener(){
return new LoggingEventListener();
}
}
@配置
公共类MongoConfig{
@豆子
公共日志EventListener(){
返回新的LoggingEventListener();
}
}
是否有任何文档或参考项目可供我参考LoggingEventListener
自定义实现bean
谢谢,这正是我要找的upsert
不会触发事件监听器:(查看