AspectJ AfterReturning 建议在 Spring AOP 中不显示输出

问题描述 投票:0回答:1

我正在尝试将 Spring AOP 与 AspectJ 结合使用,以使用 @AfterReturning 建议显示“After method”消息。我创建了一个 afterMethod 方面类,但输出未按预期显示。

这里是afterMethod.java

package AOP_Advice;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.JoinPoint;

@Aspect
public class afterMethod {
    @AfterReturning(pointcut = "execution(* employee.employee..*.print(..))", returning = "returnValue")
    public void afterReturningAdvice(JoinPoint jp, Object returnValue) {
        System.out.println("After method: This is executed after the method.");
    }

}

employee.java(不太相关,但以防万一)

package employee;

public class employee {
    private String name;
    private int age;
    private int dob;

    // constructor
    public employee() {
    }

    public employee(String name, int age, int dob) {
        super();
        this.name = name;
        this.age = age;
        this.dob = dob;
    }

    // getter & setter
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getDob() {
        return dob;
    }

    public void setDob(int dob) {
        this.dob = dob;
    }

    @Override
    public String toString() {
        return "employee [name=" + name + ", age=" + age + ", dob=" + dob + "]";
    }

    public void print() {
        System.out.println("name: " + this.name);
        System.out.println("Age: " + this.age);
        System.out.println("Day of birth: " + this.dob);
    }

    public void printThrowException() {
        throw new IllegalArgumentException();
    }
}

配置.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- Enable AspectJ Proxy -->
    <aop:aspectj-autoproxy />

    <bean id="emp1" class="employee.employee">
        <property name="name" value="Kelvin" />
        <property name="age" value="20" />
        <property name="dob" value="2003" />
    </bean>

    <bean id="emp2" class="employee.employee">
        <property name="name" value="Join" />
        <property name="age" value="25" />
        <property name="dob" value="1998" />
    </bean>

</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.springAnnotationBasedAOP</groupId>
  <artifactId>springAnnotationBasedAOP</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springAnnotationBasedAOP</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.2.25.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.25.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.2.25.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.20.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.20.1</version>
    </dependency>

  </dependencies>
</project>

和 main.java 类

package main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import employee.employee;

public class main {
    public static void main(String[] args) {
        
        // Using Annotations-based AOP
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");

        employee e1 = (employee)context.getBean("emp1");
        employee e2 = (employee)context.getBean("emp2");

        System.out.println("=====================");
        e1.print();
        System.out.println("=====================");
        e2.print();
        System.out.println("=====================");
    }
}

“我想要实现的是,在运行主类之后,它应该首先显示 print() 方法的输出,然后立即显示 afterAdvice 消息,就像这样

=====================
name: Kelvin
Age: 20
Day of birth: 2003
After method: This is executed after the method.
=====================
name: Ryna
Age: 25
Day of birth: 1998
After method: This is executed after the method.
=====================

但是 afterMethod 似乎不起作用,即使程序可以运行 有谁知道这个问题并知道如何解决它? 谢谢

java spring annotations aop
1个回答
0
投票

请将其添加到您的配置中:

  <bean id="afterMethod" class="AOP_Advice.afterMethod"/>

然后将你的切入点固定为以下任意一个:

execution(* employee.employee.print(..))
execution(* employee..*.print(..))

我还建议了解并遵守 Java 包、类和方法命名标准。包名称只能使用小写字符,类名称应使用以大写字母开头的驼峰式命名,方法名称应使用以小写字母开头的驼峰式命名。尤其可怕的是方法

main.main.main
(包、类和方法的名称相同)。 😱

我的应用程序版本如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

  <aop:aspectj-autoproxy/>

  <bean id="loggingAspect" class="de.scrum_master.spring.q77329996.aop.LoggingAspect"/>
  <bean id="emp1" class="de.scrum_master.spring.q77329996.entity.Employee">
    <property name="name" value="Kelvin"/>
    <property name="age" value="20"/>
    <property name="dob" value="2003"/>
  </bean>
  <bean id="emp2" class="de.scrum_master.spring.q77329996.entity.Employee">
    <property name="name" value="Join"/>
    <property name="age" value="25"/>
    <property name="dob" value="1998"/>
  </bean>
</beans>
package de.scrum_master.spring.q77329996.entity;

public class Employee {
  private String name;
  private int age;
  private int dob;

  public Employee(String name, int age, int dob) {
    this.name = name;
    this.age = age;
    this.dob = dob;
  }

  public Employee() {}

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getDob() {
    return dob;
  }

  public void setDob(int dob) {
    this.dob = dob;
  }

  @Override
  public String toString() {
    return "employee [name=" + name + ", age=" + age + ", dob=" + dob + "]";
  }

  public void print() {
    System.out.println("name: " + this.name);
    System.out.println("Age: " + this.age);
    System.out.println("Day of birth: " + this.dob);
  }
}
package de.scrum_master.spring.q77329996.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.JoinPoint;

@Aspect
public class LoggingAspect {
  @AfterReturning(pointcut = "execution(* de.scrum_master.spring.q77329996.entity.Employee.print(..))", returning = "returnValue")
  public void interceptPrint(JoinPoint jp, Object returnValue) {
    System.out.println(jp + " -> " + returnValue);
  }
}
package de.scrum_master.spring.q77329996.application;

import de.scrum_master.spring.q77329996.entity.Employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoApplication {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-config-q77329996.xml");
    Employee e1 = (Employee) context.getBean("emp1");
    Employee e2 = (Employee) context.getBean("emp2");
    System.out.println("=====================");
    e1.print();
    System.out.println("=====================");
    e2.print();
    System.out.println("=====================");
  }
}

控制台日志:

=====================
name: Kelvin
Age: 20
Day of birth: 2003
execution(void de.scrum_master.spring.q77329996.entity.Employee.print()) -> null
=====================
name: Join
Age: 25
Day of birth: 1998
execution(void de.scrum_master.spring.q77329996.entity.Employee.print()) -> null
=====================
© www.soinside.com 2019 - 2024. All rights reserved.