拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 @BeforeAdvise不适用于SpringBOOT中的相同方法名称

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

白鹭 - 2022-03-08 2074 0 0

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

主类

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);
    }
}

方面类

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

uj5u.com热心网友回复:

改变这个

MemberShipDAO dao = new MemberShipDAO();

对此

@Autowired MemberShipDAO dao;

Spring 对 AOP 的魔力只有在您使用 Spring 创建的实体时才会发生。在幕后,Spring 实体化了你的 dao 并用一个呼叫你的切面的代理包装它。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *