本篇内容介绍了“aop切面、注解和参数怎么获取”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
在工作中会经常使用aop,这里将aop使用基本方法,获取在切点中使用的获取参数、注解做一个样例。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AnnDemo {
String value();
boolean isAop() default true;
}
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/all")
@AnnDemo(value = "all",isAop = false)
public List<TbOrder> findAll() {
List<TbOrder> list = orderService.getOrderList();
return list;
}
@RequestMapping("/page")
@AnnDemo(value = "page")
public List<TbOrder> findPage(@RequestParam("username") String username) {
List<TbOrder> listPage = orderService.getOrdersListPage();
return listPage;
}
}
在切面中获取切点注解,方法,参数的获取
@Aspect
@Component
public class AspectDemo {
@Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..))")
public void excetionMethod() {}
@Pointcut(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")
public void excetionNote() { }
@Before("excetionMethod()")
public void testBefore(JoinPoint joinPoint) {
System.out.println("----------------------------前置通知---");
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
System.out.println(arg);
}
}
@Around(value = "execution(* com.yin.freemakeradd.controller..*(..)) && @annotation(AnnDemo)")
public Object testBeforeNote(ProceedingJoinPoint joinPoint) throws Throwable {
//用的最多通知的签名
Signature signature = joinPoint.getSignature();
MethodSignature msg=(MethodSignature) signature;
Object target = joinPoint.getTarget();
//获取注解标注的方法
Method method = target.getClass().getMethod(msg.getName(), msg.getParameterTypes());
//通过方法获取注解
AnnDemo annotation = method.getAnnotation(AnnDemo.class);
Object proceed;
//获取参数
Object[] args = joinPoint.getArgs();
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;
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SystemLog {
public String description() default "";
}
@ResponseBody
@ValidRequestBody
@RequestMapping("/login")
@SystemLog(description="登录")
public GlobalResponse login(@RequestBody @Valid User user, BindingResult bindingResult){
......
}
@Around("@annotation(com.xxx.xxx.xxx.SystemLog)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
SystemLog systemLog = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(SystemLog.class);
......
}
“aop切面、注解和参数怎么获取”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。