Plugin
The long-awaited plug-in module is here. In this module, you can use your imagination, open your mind, customize plug-ins, and develop some useful plug-ins according to your own project preferences. As long as there is value, you can submit PR, after the review, you can become an EE contributor, and the plugin can be used by developers around the world..
Notes
- Plugin modules should be as independent of other modules as possible, especially the core module
- Plug-in modules must not modify the code of other modules excessively. If there is any modification, the scope of influence needs to be evaluated
- Plug-in modules should be configurable as much as possible. When not configured, the operation and performance safety of other modules cannot be affected.
# Interceptor plugin
This plugin is contributed by the open source team member: Mr. Lu
Applicable scenarios
Some methods require special preprocessing, such as:
- Before performing the delete es data operation, you need to verify user permissions, or record operation records, etc.
- You need to append some special parameters, such as logical deletion, before executing the query In short, it is the enhancement of AOP pre-interception for various APIs provided by EE.
# Requirements Background
It is necessary to intercept the selectList method provided by this framework, and then append the logical deletion to the undeleted state in the query parameters of this method as the query condition
# Usage example
Add an interceptor, specify the intercepted method list through the @Intercepts annotation, and specify the intercepted classes, methods, parameters and other information through the @Signature annotation.
@Intercepts(
{
@Signature(type = BaseEsMapper.class, method = "selectList", args = {LambdaEsQueryWrapper.class}),
}
)
@Component
public class QueryInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("Ah, ah, I intercepted the query and added query conditions uniformly");
// The tombstone state is uniformly added to the query condition as undeleted
Object[] args = invocation.getArgs();
LambdaEsQueryWrapper<GeneralBean> arg = (LambdaEsQueryWrapper) args[0];
arg.eq(GeneralBean::getExistStatus, true);
return invocation.proceed();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Tips
- This interceptor needs to be annotated with @Component and added to the Spring container, otherwise this interceptor will not take effect.
- The full path of the implemented Interceptor is: com.xpc.easyes.sample.interceptor, not other interceptors with the same name.
# Suggestions for Improvement
The granularity is too fine and does not support wildcards. For example, if I want to intercept 5 methods prefixed with selectXXX, I need to configure 5 times through annotations to achieve this. Subsequent suggestions can support wildcard interception, such as intercepting all methods starting with select in a specified class through select*. If I have time in subsequent iterations, I will optimize this place. If developers are interested, please submit improved code and contribute PR!