【问题标题】:Spring aop is not getting triggerd for external jar method in spring boot application在 Spring Boot 应用程序中,Spring aop 没有被外部 jar 方法触发
【发布时间】:2021-07-22 06:23:49
【问题描述】:

我正在尝试对 jar 中的方法进行切入,但它没有被正确触发

我的休息端点代码如下:

package com.example.log.javatpoint;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test{

    @Autowired
    Operation op;

    private static final Logger LOG = LogManager.getLogger(Test.class);

    @RequestMapping(value = "/customs", method = RequestMethod.GET)
    public String custom() {
        op.msg();  
        op.display(); 

        LOG.info("Hello World");
        LOG.info("Hello {0}", "World 2");

        return "custom";
    }
}

操作类:

@Component
public  class Operation{
    public void msg(){System.out.println("msg() is invoked");}
    public void display(){System.out.println("display() is invoked");}
}
@Aspect
@Component
public class TrackOperation
{
    @Pointcut("execution(* Operation.*(..))")
    public void abcPointcut(){}

    @Around("abcPointcut()")
    public Object myAdvice(ProceedingJoinPoint pjp) throws Throwable 
    {
        System.out.println("Additional Concern Before calling actual method");
        Object obj=pjp.proceed();
        System.out.println("Additional Concern After calling actual method");
        return obj;
    }

    @Pointcut("execution(* org.apache.logging.log4j.LogManager.info(..))")
    public void abcPointcutLog(){}

    @Around("abcPointcutLog()")
    public Object myAdviceLog(ProceedingJoinPoint pjp) throws Throwable 
    {
        System.out.println("Additional Concern Before calling actual method");
        Object obj=pjp.proceed();
        System.out.println("Additional Concern After calling actual method");
        return obj;
    }
}

注意:切入点适用于 Operation 类,因为切入点不适用于 org.apache.logging.log4j.LogManager 尝试还提供 org.apache.logging.log4j.Logger 切入点。

我希望输出为:

Additional Concern Before calling actual method
2019-09-24 12:28:58.540  INFO 10076 --- [nio-8080-exec-1] com.example.log.javatpoint.Test          : Hello World
Additional Concern After calling actual method
Additional Concern Before calling actual method
2019-09-24 12:28:58.540  INFO 10076 --- [nio-8080-exec-1] com.example.log.javatpoint.Test          : Hello {0}
Additional Concern After calling actual method

但实际输出是:

2019-09-24 12:28:58.540  INFO 10076 --- [nio-8080-exec-1] com.example.log.javatpoint.Test          : Hello World
2019-09-24 12:28:58.540  INFO 10076 --- [nio-8080-exec-1] com.example.log.javatpoint.Test          : Hello {0}

【问题讨论】:

    标签: spring-aop


    【解决方案1】:

    这个问题是“经典”,已经在这里被问过很多次了......

    在使用您不知道的工具之前,请阅读 Spring AOP 手册。它会告诉你Spring AOP can only be applied to Spring components/beans,而不是非 Spring POJO。为此,您需要完整的 AspectJ,您可以在 Spring 中使用或完全不使用 Spring。

    Log4J 类不是 Spring 组件,因此上述内容适用于您的情况。 Here 您可以找到有关如何使用 AspectJ 加载时编织 (LTW) 而不是 Spring AOP 的信息。

    【讨论】:

    • 您能否提供代码,说明需要在此处更改以实现我的输出。
    • 不是代码,是配置。您是否关注了我关于 AspectJ LTW 的链接?
    • 我尝试提供@EnableLoadTimeWeaving 但没有成功。如果你能给我一个工作代码会更有帮助。尝试了该网站上的所有内容。
    • 您在启动 JVM 时需要 -javaagent:/path/to/aspectjweaver.jar@EnableLoadTimeWeaving 只能与一些相当棘手的容器配置结合使用,Java 代理是最安全的选择。您还需要 aop.xml 在正确的位置,但这都写在手册中。我再说一遍:这不是关于更改代码,而是关于更改配置。您没有在这里分享您的配置或 JVM 命令行,那么我如何告诉您具体要更改什么?你也需要自己做一些工作。
    • 如果您分享完整的MCVE,包括。 GitHub上的Maven POM,我可以看看。但请先尝试自己,不要将我作为不完整阅读文档的捷径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2020-01-19
    相关资源
    最近更新 更多