How can spring aop obtain the parameters, method names, return values, exceptions and other information of pointcut related methods in the tangent class

aop idea can help us to decouple the code. For example, as we mentioned earlier, log code and business layer code are completely independent and integrated through spring aop proxy class. In the aspect class, we can also get the relevant information of the original pointcut through the interface provided by spring.

First of all, let's start with code

Business layer code StudentServiceImpl

public class StudentServiceImpl implements StudentService {

    public int addStudent(Student student) throws Exception {
            System.out.println("addStudent...Business layer code execution...");
            return 1;

    public int deleteStudent(Integer id) throws Exception{

            System.out.println("deleteStudent...Business layer code execution...");
            int i = 1/0;
            return 0;


Faceted class StudentServiceLogger

public class StudentServiceLogger {

    @Before("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
    public void doBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"Before method execution...");
        System.out.println("The parameters are:"+ Arrays.asList(args));

    @After("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
    public void doAfter(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"After method execution...");

    @AfterReturning(value = "execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )" , returning = "returning")
    public void doReturn(JoinPoint joinPoint,Object returning){
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"Method, the return value is:"+returning);


    @AfterThrowing(value = "execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )",throwing = "ex")
    public void doThrow(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"Method exception, exception information is:"+ex.getMessage());


Test class AopTest

public class AopTest {

    ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

    public void test1() throws Exception {
        StudentService studentService = ac.getBean("studentService",StudentService.class);
        studentService.addStudent(new Student());


Finally, log output results

Before the addStudent method executes
 The parameters are: [com.wuwl.domain.Student@7e5c856f]
addStudent... Business layer code execution
 After the addStudent method executes
 The addStudent method returns with a return value of: 1
deleteStudent method before execution
 Parameters are: [1]
deleteStudent... Business layer code execution
 After deleteStudent method execution
 deleteStudent method exception, exception information is. / by zero

spring is well encapsulated in the org.aspectj.lang.JoinPoint interface. It should be noted that there is also an interface named in this way under the org.aopalliance.intercept package. Don't get it wrong.
The joinPoint.getArgs() method can return the parameter information of the cut in method. The return value is an array, which can be obtained by traversal. The return value of the joinPoint.getSignature() method is the method signature, and the signature interface includes information such as the method name.
If you need to get the return value of the cut in method or exception information in the notification method, you need to use the corresponding attribute of the corresponding annotation. Click the @ AfterReturning annotation to see a String returning() default ""; property, add a return value parameter to the notification method, and then declare the parameter as the return value of the cut in method in the annotation. For the operation method, please refer to the above code. Similarly, you can use the String throwing() default '' property of the @ AfterThrowing annotation to get the exception information thrown by the pointcut method.

Published 92 original articles, won praise 13, visited 20000+
Private letter follow

Tags: Spring xml Attribute

Posted on Mon, 09 Mar 2020 23:45:34 -0400 by interactive