1、配置pom.xml。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2、AOPConfig.java
package com.idodo.demo.conf; import java.util.Arrays; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.context.annotation.Configuration; @Aspect @Configuration public class AOPConfig { // Controller的所有方法,在执行前后都会进入functionTestAop方法 @Around("@within(org.springframework.stereotype.Controller) ") public Object functionTestAop(final ProceedingJoinPoint pjp) throws Throwable { try { Object[] args = pjp.getArgs(); System.out.println("前:args:" + Arrays.asList(args)); // 继续处理原有调用 Object o = pjp.proceed(); System.out.println("后:return :" + o); return o; } catch (Throwable e) { throw e; } } }
3、控台台输出。
前:args:[jzh]
后:return :hello jzh