java - 为什么 After Advice 在方法调用之前得到打印

标签 java annotations aop spring-aop aspect

我的 Spring AOP 程序未按预期运行。 我在下面创建了简单的AOP注释程序,但输出不是我想象的。

节目:

Beans.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-3.0.xsd 
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

 <aop:aspectj-autoproxy/>


 <bean id="student"  class="com.surajhome.practice.spring.Student" >
    <property name="name" value="Suraj Kudale"></property>
    <property name="age" value="27"></property>
 </bean>  

  <bean id="logging" class="com.surajhome.practice.spring.Logging"></bean>


 </beans>

学生.java

包 com.surajhome.practice.spring;

public class Student {

    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;
    }
    String name;
    int age;



}

日志记录.Java

package com.surajhome.practice.spring;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Logging {

    @Pointcut("execution(* com.surajhome.practice.spring.*.*(..) )")
    public void selectAll()
    {

    }
    @After("selectAll()")
    public void afterAdvice()
    {
        System.out.println("After Advice called");
    }

    @Before("selectAll()")
    public void beforeAdvice()
    {
        System.out.println("Before Advice called");
    }

    public void afterReturningAdvice()
    {
        System.out.println("After Returning Advice called");
    }

    public void afterThrowingException()
    {
        System.out.println("After Exception Advice called");
    }

}

MainApp.java

package com.surajhome.practice.spring;

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

public class MainApp {

    public static void main(String[] args) {

        ApplicationContext appContext=new ClassPathXmlApplicationContext("Beans.xml");

        Student std=(Student) appContext.getBean("student");


        System.out.println(std.getName());
        System.out.println(std.getAge());

    }
}

输出:

调用建议之前
调用建议后
苏拉吉·库代尔
在调用建议之前
调用建议后
27

应该是:
在调用建议之前
苏拉吉·库代尔
调用建议后
在调用建议之前
27
调用建议后

最佳答案

想想当您调用 System.out.println(std.getName()); 时发生的流程,首先调用get name的@Before方法,然后get name返回一个值,然后调用@After,然后System.out.println获取字符串并打印它

关于java - 为什么 After Advice 在方法调用之前得到打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49843024/

相关文章:

java - 这个java方法对按位运算有什么作用?

java - 将对象转换为接口(interface)未实现?

java - 为什么 List.toArray() 不是通用的?

iphone - 如何旋转MKMapView并保持Annotation和 View 不旋转?

java - Jpa注解

ruby-on-rails - 使用面向方面的编程进行Rails异常处理

java.lang.UnsupportedClassVersionError - 不同版本的 JDK 和 JRE

symfony 路由注解需求约束

c# - MsTest 数据驱动 : ignore specific datarows via testattribute?

java - 如何在不破坏 Guice-AOP 方法拦截的情况下在 Guice 注入(inject)中提供参数?