ParamResolverAdvice
允许用户在ParamResolver
参数解析器解析参数的前后添加业务逻辑以及修改解析后的参数。
接口定义
public interface ParamResolverAdvice {
/**
* This method is called around {@link ParamResolver#resolve(RequestContext)}.
*
* @param context context
* @return resolved arg value
* @throws Exception exception
*/
Object aroundResolve(ParamResolverContext context) throws Exception;
}
自定义ParamResolverAdvice
与ParamResolver
相同,ParamResolverAdvice
自定应时同样需要实现ParamResolverAdviceAdapter
或ParamResolverAdviceFactory
接口
方式1:实现 ParamResolverAdviceAdapter
接口定义
public interface ParamResolverAdviceAdapter extends ParamPredicate, ParamResolverAdvice, Ordered {
@Override
default int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
方式2:实现ParamResolverAdviceFactory
接口定义
public interface ParamResolverAdviceFactory extends ParamPredicate, Ordered {
/**
* Creates an instance of {@link ParamResolverAdvice} for given handler method.
*
* @param param method
* @param resolver paramResolver associated with this parameter
* @return advice
*/
ParamResolverAdvice createResolverAdvice(Param param, ParamResolver resolver);
}
ParamResolverAdviceAdapter
接口以及ParamResolverAdviceFactory
接口与ParamResolver
中的ParamResolverAdapter
接口以及ParamResolverFactory
接口的使用方式相同,这里不过多赘述。
Note
ParamResolverAdvice
与ParamResolver
生命周期是相同的, 即应用初始化的时候便会决定每个参数的ParamResolverAdvice