@BeforeAdvise 不适用于 Spring BOOT 中的相同方法名称

@BeforeAdvise is not working for same method names in Spring BOOT

我正在学习 Spring AOP 并尝试为不同 类 中的两种不同方法调用 @BeforeAdvise。但是在第一次调用中添加的日志只被打印出来。请检查下面的代码,让我知道这里有什么不正确的地方。

MainClass

package com.example.aspects;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.example.aspects.AopClasses.DemoConfig;

@SpringBootApplication
public class AspectsApplication {
    public static void main(String[] args) {
        SpringApplication.run(AspectsApplication.class, args);
    }
}

AspectClass

package com.example.aspects.Acpects;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyLoggingDemo {

    private static final Logger logger = LogManager.getLogger(MyLoggingDemo.class);

    @Before("execution(public String test())")
    public void beforeCheck() {
        logger.info("Before Check !!!!!!!!!!!! "+System.currentTimeMillis() );  
    }   
}

配置类

package com.example.aspects.AopClasses;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.example.aspects")
public class DemoConfig {

}

TestClass- 第一次调用函数的地方

package com.example.aspects.AopClasses;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class tesTClass {

    private static final Logger logger = LogManager.getLogger(tesTClass.class);
     MemberShipDAO dao = new MemberShipDAO();

    @RequestMapping(value = "/")
    public String test() {
        logger.info("Hey I am here");
        return "HEy There I am Working !!";
    }
    
    @RequestMapping(value ="/test")
    public String testing() {
        logger.info("MemberShipDAO Call");
    return  dao.test();     // method with same name called here again
    }   
}

MemberShipDAO - 在这里再次声明测试方法

package com.example.aspects.AopClasses;

import org.springframework.stereotype.Component;

@Component
public class MemberShipDAO {

    public String test() {
        return "HEy!!";
    }
}

控制台

2022-01-18 11:37:52.794  INFO 8032 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-01-18 11:37:52.796  INFO 8032 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-01-18 11:37:52.802  INFO 8032 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
2022-01-18 11:37:52.811  INFO 8032 --- [nio-8080-exec-1] c.example.aspects.Acpects.MyLoggingDemo  : Before Check !!!!!!!!!!!! 1642486072811
2022-01-18 11:37:52.854  INFO 8032 --- [nio-8080-exec-1] c.example.aspects.AopClasses.tesTClass   : Hey I am here
2022-01-18 11:37:57.383  INFO 8032 --- [nio-8080-exec-3] c.example.aspects.AopClasses.tesTClass   : MemberShipDAO Call

改变这个

MemberShipDAO dao = new MemberShipDAO();

至此

@Autowired MemberShipDAO dao;

Spring 的 AOP 魔力只有在您使用 Spring 创建的实例时才会发生。在幕后,Spring 实例化了你的 dao 并用一个 proxy 包装了它,它调用了你的方面。