Interceptors
Interceptors in MongoPlus allow you to intercept calls to the executor and modify the input parameters.
Note
Interceptors need to be registered as Spring Beans.
Standard Interceptor
com.mongoplus.interceptor.Interceptor
- This interface defines a standard interceptor. All interceptors should implement this interface and can selectively implement its methods.
executeSave(documentList): Intercept add operationsexecuteRemove(filter): Intercept delete operationsexecuteUpdate(queryBasic, updateBasic): Intercept update operationsexecuteQuery(queryBasic, projectionList, sortCond): Intercept query operationsexecuteAggregate(aggregateConditionList): Intercept aggregation operationsexecuteCount(queryBasic, countOptions): Intercept count operationsexecuteBulkWrite(writeModelList): Intercept bulk write operationsexecuteSave(documentList, collection): Intercept add operations with collectionexecuteRemove(filter, collection): Intercept delete operations with collectionexecuteUpdate(queryBasic, updateBasic, collection): Intercept update operations with collectionexecuteQuery(queryBasic, projectionList, sortCond, collection): Intercept query operations with collectionexecuteAggregate(aggregateConditionList, collection): Intercept aggregation operations with collectionexecuteCount(queryBasic, countOptions, collection): Intercept count operations with collectionexecuteBulkWrite(writeModelList, collection): Intercept bulk write operations with collection
Example
1. Implement the Interceptor Interface
The interceptor class must implement the Interceptor interface and be registered as a Spring Bean.
// Register as Spring Bean
@Component
public class CustomInterceptor implements Interceptor {}2. Implement Methods
Selectively implement methods of the Interceptor interface to intercept and modify parameters. Here we only implement the save interceptor.
// Register as Spring Bean
@Component
public class CustomInterceptor implements Interceptor {
@Override
public List<Document> executeSave(List<Document> documentList) {
log.info("Logging input values for save operation");
documentList.forEach(System.out::println);
return documentList;
}
}Advanced Interceptor
com.mongoplus.interceptor.AdvancedInterceptor
Compared to standard interceptors, advanced interceptors provide higher privileges and can directly return data, similar to MyBatis interceptors.
Advanced interceptors form a chain of responsibility together with all registered standard interceptors. They are closer to the underlying execution layer.
Example
@Bean
public class OptimisticLockerInterceptor implements AdvancedInterceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// Implement your business logic here
// Continue execution of other interceptors
return invocation.proceed();
}
}
