1.简介
eBatis中拦截器的加载通过SPI方式实现,只需要提供的目标类实现io.manbang.ebatis.core.interceptor.Interceptor接口,在目标类上加上注解@AutoService(Interceptor.class),由auto-service生成。
2.interceptor开发
在项目目录“/src/main/java/com/example/es”下新建interceptor目录,并在interceptor目录下新建EmployeeInterceptor类,EmployeeInterceptor类需要实现Interceptor接口,具体代码如下。
@Slf4j
@AutoService(Interceptor.class)
public class EmployeeInterceptor implements Interceptor {
@Override
public int getOrder() {
return 0;
}
@Override
public void handleException(Throwable throwable) {
log.error("throwable:{}", throwable);
}
@Override
public <T extends ActionRequest> void postRequest(RequestInfo<T> requestInfo) {
log.info("requestInfo1:{}", requestInfo);
log.info("requestInfo2:{}", requestInfo.actionRequest());
log.error("requestInfo employeeId:{}", ContextHolder.getString("employeeId"));
}
@Override
public <T extends ActionRequest> void preResponse(PreResponseInfo<T> preResponseInfo) {
log.error("preResponseInfo employeeId:{}", ContextHolder.getString("employeeId"));
}
@Override
public <T extends ActionRequest, R extends ActionResponse> void postResponse(PostResponseInfo<T, R> postResponseInfo) {
log.info("postResponseInfo:{}", postResponseInfo.actionResponse());
log.error("postResponseInfo employeeId:{}", ContextHolder.<String>getValue("employeeId"));
}
}
3.测试
启动项目,然后在postman中请求“http://localhost:8080/employee/queryEmployeePage”,成功后可在控制台输出如下的信息。