@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RuleArea {
}
@Aspect
@Component
public class RuleAreaAspect {
@Autowired
private PlatFeignClient platFeignClient;
// 配置织入点
@Pointcut("@annotation(com.util.ruleAreaAspect.RuleArea)")
public void PoinCut() {
}
// 环绕通知
@Around(value = "PoinCut()")
public Object sayAround(ProceedingJoinPoint joinPoint) throws Throwable {
// 下面两个数组中,参数值和参数名的个数和位置是一一对应的。
Object[] objs = joinPoint.getArgs();
String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames(); // 参数名
Map<String, Object> paramMap = new HashMap<String, Object>();
for (int i = 0; i < objs.length; i++) {
if (!(objs[i] instanceof ExtendedServletRequestDataBinder) && !(objs[i] instanceof HttpServletResponseWrapper)) {
paramMap.put(argNames[i], objs[i]);
}
}
//此处的request必须和下面调用时的request完全一致
HttpServletRequest request = (HttpServletRequest) paramMap.get("request");
//从plat中获取到省和id保存进去
Integer userId = Utilties.getUserId(request);
List<SocAreaEntity> list = platFeignClient.queryProvince(userId);
List<String> prov_id_list = list.stream().map(e -> String.valueOf(e.getA_id())).collect(Collectors.toList());
//此处的alarm必须和下面调用时的alarm完全一致
Object alarmRule = paramMap.get("alarm");
Class<?> clazz = alarmRule.getClass();
Method setUser_id = clazz.getDeclaredMethod("setUser_id",Integer.class);
Method setProv_id = clazz.getDeclaredMethod("setProv_id",String.class);
setUser_id.invoke(alarmRule,userId);
setProv_id.invoke(alarmRule,String.join(",",prov_id_list));
Object result = joinPoint.proceed();// 执行方法
return result;
}
}
调用时:
@RuleArea
@RequestMapping(value = "/add1", method = {RequestMethod.GET, RequestMethod.POST})
//此处的alarm必须和上面注解中的alarm完全一致
public String add1(Alarm1 alarm, HttpServletRequest request) {
return alarmFeignClient.addAlarmRule(alarm);
}