Plugin
The long-awaited plug-in module is here, in this module you can use their imagination, brainstorming, custom plug-ins, according to their own project preferences to develop some good plug-ins, as long as there is value, can be submitted to the PR, the review can become EE contributors, the plug-in can be used by developers worldwide.
Caution
- Plugin modules should be independent of other modules, especially the core module
- Plugin modules should not overly modify other modules code, if there are changes, need to assess the impact of the scope
- Plug-in module as far as possible to do configurable, not configured to open, not to affect the operation of other modules and performance security, etc.
# Interceptor plug-in
This plugin was contributed by open source team member: Mr. Lu
Applicable scenarios
Need to do special preprocessing for some methods, for example:
- Need to delete data in the implementation of the operation before the need to verify user permissions, or record the operation records, etc.
- Need to execute the query before the append some special parameters, such as logical deletion In short, it is a variety of APIs provided by EE to enhance the AOP pre-interception.
# requirements background
need to intercept the framework provides the selectList method , and then the query parameters in this method to append the logical deletion of the undeleted state as a query condition
# Example of use
Add a new interceptor , the @Intercepts annotation specifies the intercepted method list , the @Signature annotation specifies the intercepted class , method , parameters and other information .
@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("Ahhhhhh, I intercepted the query, add the query condition uniformly");
// query conditions uniformly add logic to delete the state as not deleted
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
提示
- you need to add @Component annotation to this interceptor and add it to Spring container, otherwise this interceptor will not work.
- 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, does not support wildcards, for example, I want to intercept 5 methods prefixed with selectXXX, I need to configure 5 times by annotation to achieve, Subsequent suggestions can support wildcard interception, such as through select * intercept all methods in the specified class to select first. Subsequent iterations if I have time to optimize here, if there are developers interested in submitting improved code, contribute PR!