1: (Understanding) There is a parent class in the target object A, there is a method action in the parent class, and the target object A does not re-action. Other methods of the target object use spring aop-related annotations. When the action method of target object A is called, the proxy object is still calling
2: (Understanding) There is a parent class in the target object A, and there is a method action in the parent class, and the target object A re-actions. Other methods of the target object use spring aop-related annotations. When the action method of target object A is called, the target object is calling
3: The following is an example, you can verify by yourself
/**
模板类
**/
public abstract class AbstractHandler<T, I> {
public static final String JSON_STR = "json";
/**
* 参数转换
*
* @param request 入参
* @return 业务参数
*/
protected abstract T cast(ApiInvokeRequest<I> request);
protected abstract void validation(ApiInvokeRequest<I> request);
protected abstract void preDoBiz(T t);
protected abstract void postDoBiz(String assessmentNo, String result);
protected String doCall(String serviceName, String bizContent) {
JSONObject paramObj = new JSONObject();
paramObj.put("serviceName", serviceName);
paramObj.put("bizContent", bizContent);
String url="http://***";
//通过SpringUtil取
String result = AsyncHttpUtil.doPost(url, paramObj.toJSONString());
return result;
}
final public Response<String> action(ApiInvokeRequest<I> request) {
Response<String> response = new Fail();// 默认失败,Fail 不提供出来了
String result = null;
AssessmentApiRecordMongoDO assessmentApiRecordMongoDO = null;
try {
// 1.通用校验
validation(request);
// 2.转换
T bizObject = cast(request);
// 3: 执行业务前的处理
preDoBiz(bizObject);
// 4.执行业务逻辑
result = doCall(request.getApiNameEnum().getServiceName(), getBizContent(request, bizObject));
if (StringUtils.isNotEmpty(result)) {
}
} catch (Exception e) {
response = new Fail();
} finally {
}
return response;
}
/**
**/
@service
public class AHandlerTemplate<T,I> extends AbstractGatewayHandler<T,I>{
@Override
protected T cast(ApiInvokeRequest<I> request) {
return null;
}
@Override
protected void validation(ApiInvokeRequest<I> request) {
}
@Async
@Override
protected void preDoBiz(T bizObject) {
}
@Override
protected void postDoBiz(String assessmentNo,String result) {
}
}
@RestController
public class TestController{
@Resoure
private AHandlerTemplate aHandlerTemplate;
public void test(){
// 这些只是告诉你们调用的情况。不要纠结具体的细节。我这都是手打的
ApiInvokeRequest<> apiInvokeRequest;
aHandlerTemplate.action(apiInvokeRequest);
上面这个调用方式,aHandlerTemplate是一个代理对象,代理对象调用到action的时候,
调试到AbstractHandler类的action方法时,这个对象还是代理对象。而不是目标对象。
为什么?因为spring 给我们生成的代理对象,AHandlerTemplateCGlig$$ 一个这样的对象,只对AHandlerTemplate 中的方法有效,action方法不在AHandlerTemplate 在。所以AHandlerTemplateCGlig$$不会对action进行增强,那么调用action方法时,当前的this对象依然就是代理对象。
如何验证呢?
把spring生成的代理对象,打印出来。就一目了然。以下就是把代理对象生成的办法
public static void main(String[] args) {
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\code");
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(AHandlerTemplate.class);
SendSurveyInfoHandler proxy= (SendSurveyInfoHandler) enhancer.create();
}
}
}