Skip to content
广告❤️成为赞助商

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 operations
  • executeRemove(filter): Intercept delete operations
  • executeUpdate(queryBasic, updateBasic): Intercept update operations
  • executeQuery(queryBasic, projectionList, sortCond): Intercept query operations
  • executeAggregate(aggregateConditionList): Intercept aggregation operations
  • executeCount(queryBasic, countOptions): Intercept count operations
  • executeBulkWrite(writeModelList): Intercept bulk write operations
  • executeSave(documentList, collection): Intercept add operations with collection
  • executeRemove(filter, collection): Intercept delete operations with collection
  • executeUpdate(queryBasic, updateBasic, collection): Intercept update operations with collection
  • executeQuery(queryBasic, projectionList, sortCond, collection): Intercept query operations with collection
  • executeAggregate(aggregateConditionList, collection): Intercept aggregation operations with collection
  • executeCount(queryBasic, countOptions, collection): Intercept count operations with collection
  • executeBulkWrite(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.

java
// 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.

java
// 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

java
@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();
    }
}