动态代理
AOP切面代码
被代理对象
Object target = joinPoint.getTarget();
当前对象
Object aThis = joinPoint.getThis();
这里拿到的方法和反射的方法获取的不是一样的
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
Method method1 = null;
try {
method1 = target.getClass().getMethod(method.getName(), method.getParameterTypes());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
动态代理 修改注解上的方法
import com.ruijie.businesslog.annotation.SendFeishu;
import com.ruijie.framework.base.config.BaseEnvironmentConfigration;
import com.ruijie.purchase.exception.PurchaseAppCode;
import com.ruijie.purchase.exception.scm.PmdException;
import com.ruijie.purchase.service.OpsDataService;
import com.ruijie.purchase.service.PurchaseMinorDemandHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.function.Consumer;
/**
* @menu TODO
* @BelongsProject: minor-purchase
* @BelongsPackage: com.ruijie.purchase.ops.job
* @Author: [email protected]
* @CreateTime: 2023-02-15 15:13
* @Version: 1.0
*/
@Component
@Slf4j
public class OpsSolidifiedVersionScheduleAfterJob {
@Autowired
private BaseEnvironmentConfigration baseEnvironmentConfigration;
@Autowired
private OpsDataService opsDataService;
@Autowired
private PurchaseMinorDemandHistoryService purchaseMinorDemandHistoryService;
@Scheduled(cron = "${purchase.email.Job.solidifiedVersionAfter.cron:0 5 22 ? * 2}")
@SendFeishu("XXX-${version}版本数据上传校验")
public String solidifiedVersionScheduleAfter() {
String version = purchaseMinorDemandHistoryService.getMaxVersion();
try {
//生成代理增加版本控制 修改@SendFeishu("XXX-${version}版本数据上传校验") 中的 ${version}
sendFeishuProxy(version);
} catch (Exception e) {
throw new PmdException(new PurchaseAppCode("",e.toString()));
}
// String currentEnv = baseEnvironmentConfigration.getCurrentEnv();
// //不在开发环境运行
// if (EnvEnum.dev.name().equals(currentEnv)) {
// log.info("定时任务不在开发环境运行!");
// return "定时任务不在开发环境运行!";
// }
return opsDataService.solidifiedVersionScheduleAfter(version);
}
private void sendFeishuProxy(String version) throws Exception {
Class<? extends OpsSolidifiedVersionScheduleAfterJob> clz = this.getClass();
//int i = 1/0;
Method solidifiedVersionScheduleAfter = clz.getMethod("solidifiedVersionScheduleAfter");
SendFeishu annotation = solidifiedVersionScheduleAfter.getAnnotation(SendFeishu.class);
if (annotation != null) {
modifyFiled(annotation, map -> {
String value = (String) map.get("value");
value = value.replace("${version}", version == null ? "" : version);
map.put("value", value);
});
}
}
private void modifyFiled(SendFeishu annotation, Consumer<Map> consumer) throws Exception {
//生成代理类对象
InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
Field annotationValues = invocationHandler.getClass().getDeclaredField("memberValues");
annotationValues.setAccessible(true);
Map map = (Map) annotationValues.get(invocationHandler);
consumer.accept(map);
}
}
标签:purchase,ruijie,代理,version,import,注解,动态,com,annotation
From: https://www.cnblogs.com/yexuba/p/17124339.html