使⽤spring通过aop获取⽅法参数和参数值
⽬录
spring通过aop获取⽅法参数和参数值
⾃定义注解
切⾯
aop切⾯注解、参数获取
1、定义需要切⾯的注解
2、在需要进⾏切⾯的⽅法标注注解
3、定义切⾯
spring通过aop获取⽅法参数和参数值
⾃定义注解
package com.xiaolc.aspect;
import java.lang.annotation.*;
/**
* @author lc
* @date 2019/9/10
*/
@Documented
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LiCheng {
}
切⾯
package com.xiaolc.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import DefaultParameterNameDiscoverer;
import ParameterNameDiscoverer;
import org.springframework.stereotype.Component;
import flect.Method;
import java.util.HashMap;
import java.util.Map;
/
**
* 获取⽅法上的注解值
*/
@Component
@Aspect
public class AuditAnnotationAspect {
@Around("@annotation(liCheng))")
private static Map getFieldsName(ProceedingJoinPoint joinPoint,LiCheng liCheng) throws ClassNotFoundException, NoSuchMethodException { String classType = Target().getClass().getName();
String methodName = Signature().getName();
// 参数值
Object[] args = Args();
Class<?>[] classes = new Class[args.length];
for (int k = 0; k < args.length; k++) {
if (!args[k].getClass().isPrimitive()) {
// 获取的是封装类型⽽不是基础类型
String result = args[k].getClass().getName();
Class s = (result);
classes[k] = s == null ? args[k].getClass() : s;
}
}
ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer();
/
/ 获取指定的⽅法,第⼆个参数可以不传,但是为了防⽌有重载的现象,还是需要传⼊参数的类型
Method method = Class.forName(classType).getMethod(methodName, classes);
// 参数名
String[] parameterNames = ParameterNames(method);
// 通过map封装参数和参数值
HashMap<String, Object> paramMap = new HashMap();
for (int i = 0; i < parameterNames.length; i++) {
paramMap.put(parameterNames[i], args[i]);
System.out.println("参数名:"+parameterNames[i]+"\n参数值"+args[i]);
}
return paramMap;
}
private static HashMap<String, Class> map = new HashMap<String, Class>() {
{
put("java.lang.Integer", int.class);
put("java.lang.Double", double.class);
put("java.lang.Float", float.class);
put("java.lang.Long", Long.class);
put("java.lang.Short", short.class);
put("java.lang.Boolean", boolean.class);
put("java.lang.Char", char.class);
}
};
}
aop切⾯注解、参数获取
在⼯作中会经常使⽤aop,这⾥将aop使⽤基本⽅法,获取在切点中使⽤的获取参数、注解做⼀个样例。
1、定义需要切⾯的注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AnnDemo {
String value();
boolean isAop() default true;
}
2、在需要进⾏切⾯的⽅法标注注解
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/all")
@AnnDemo(value = "all",isAop = false)
public List<TbOrder> findAll() {
List<TbOrder> list = OrderList();
return list;
}
@RequestMapping("/page")
@AnnDemo(value = "page")
public List<TbOrder> findPage(@RequestParam("username") String username) {
List<TbOrder> listPage = OrdersListPage();
return listPage;
}
}
3、定义切⾯
在切⾯中获取切点注解,⽅法,参数的获取
springboot aop
@Aspect
@Component
public class AspectDemo {
@Pointcut(value = "execution(* com.ller..*(..))")
public void excetionMethod() {}
@Pointcut(value = "execution(* com.ller..*(..)) && @annotation(AnnDemo)")
public void excetionNote() { }
@Before("excetionMethod()")
public void testBefore(JoinPoint joinPoint) {
System.out.println("----------------------------前置通知---");
Object[] args = Args();
for (Object arg : args) {
System.out.println(arg);
}
}
@Around(value = "execution(* com.ller..*(..)) && @annotation(AnnDemo)") public Object testBeforeNote(ProceedingJoinPoint joinPoint) throws Throwable {
//⽤的最多通知的签名
Signature signature = Signature();
MethodSignature msg=(MethodSignature) signature;
Object target = Target();
//获取注解标注的⽅法
Method method = Class().Name(), ParameterTypes());
//通过⽅法获取注解
AnnDemo annotation = Annotation(AnnDemo.class);
Object proceed;
//获取参数
Object[] args = Args();
System.out.println(annotation.value());
System.out.println(annotation.isAop());
for (Object arg : args) {
System.out.println(arg);
}
if (Objects.isNull(annotation) || !annotation.isAop()) {
System.out.println("⽆需处理");
proceed = joinPoint.proceed();
}else {
System.out.println("进⼊aop判断");
proceed = joinPoint.proceed();
if(proceed instanceof List){
List proceedLst = (List) proceed;
if(!CollectionUtils.isEmpty(proceedLst)){
TbOrder tbOrder = new TbOrder();
tbOrder.setPaymentType("fffffffffffffffffff");
ArrayList<TbOrder> tbOrderLst = new ArrayList<>();
tbOrderLst.add(tbOrder);
return tbOrderLst;
}
}
System.out.println(proceed);
}
return proceed;
}
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论