拦截所有对 MongoDB Spring Boot 的写入调用
Intercept all write calls to MongoDB Spring Boot
我正在与 Spring Boot 2.1.5
合作。我正在尝试编写一个服务来拦截对 MongoDB 的所有写入调用。基本上类似于 DBMS 中的 Op 日志,在任何数据写入 DB 甚至更新或删除之前,我捕获正在更新或创建的文档。
这可能吗?如果有怎么办?
It's for the DB calls made from within my application. Because you see let's say you have a large application containing 100 APIs or so, it's tough to actually integrate an OpLog (DB updates, writes, deletes) in all API controllers or services, instead write up an interceptor or aspect that gets triggered before functions of the MongoRepository or MongoTemplate is called.
您可以扩展 org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener 并实现您的自定义侦听器。
Here 是 spring 相关的文档。
The following callback methods are present in AbstractMappingEventListener:
onBeforeConvert: Called in MongoTemplate insert, insertList, and save operations before the object is converted to a Document by a MongoConverter.
onBeforeSave: Called in MongoTemplate insert, insertList, and save operations before inserting or saving the Document in the database.
onAfterSave: Called in MongoTemplate insert, insertList, and save operations after inserting or saving the Document in the database.
onAfterLoad: Called in MongoTemplate find, findAndRemove, findOne, and getCollection methods after the Document has been retrieved from the database.
onAfterConvert: Called in MongoTemplate find, findAndRemove, findOne, and getCollection methods after the Document has been retrieved from the database was converted to a POJO.
如果想记录所有的操作,可以定义一个org.springframework.data.mongodb.core.mapping.event.LoggingEventListener的bean。
@Configuration
public class MongoConfig {
@Bean
public LoggingEventListener<Object> listener(){
return new LoggingEventListener();
}
}
我正在与 Spring Boot 2.1.5
合作。我正在尝试编写一个服务来拦截对 MongoDB 的所有写入调用。基本上类似于 DBMS 中的 Op 日志,在任何数据写入 DB 甚至更新或删除之前,我捕获正在更新或创建的文档。
这可能吗?如果有怎么办?
It's for the DB calls made from within my application. Because you see let's say you have a large application containing 100 APIs or so, it's tough to actually integrate an OpLog (DB updates, writes, deletes) in all API controllers or services, instead write up an interceptor or aspect that gets triggered before functions of the MongoRepository or MongoTemplate is called.
您可以扩展 org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener 并实现您的自定义侦听器。
Here 是 spring 相关的文档。
The following callback methods are present in AbstractMappingEventListener:
onBeforeConvert: Called in MongoTemplate insert, insertList, and save operations before the object is converted to a Document by a MongoConverter.
onBeforeSave: Called in MongoTemplate insert, insertList, and save operations before inserting or saving the Document in the database.
onAfterSave: Called in MongoTemplate insert, insertList, and save operations after inserting or saving the Document in the database.
onAfterLoad: Called in MongoTemplate find, findAndRemove, findOne, and getCollection methods after the Document has been retrieved from the database.
onAfterConvert: Called in MongoTemplate find, findAndRemove, findOne, and getCollection methods after the Document has been retrieved from the database was converted to a POJO.
如果想记录所有的操作,可以定义一个org.springframework.data.mongodb.core.mapping.event.LoggingEventListener的bean。
@Configuration
public class MongoConfig {
@Bean
public LoggingEventListener<Object> listener(){
return new LoggingEventListener();
}
}