Spring aop Spring aop未在Spring引导应用程序中获取外部jar方法的触发器

Spring aop Spring aop未在Spring引导应用程序中获取外部jar方法的触发器,spring-aop,Spring Aop,我试图在jar中为一个方法设置切点,但它并没有被正确触发 我的rest端点代码如下所示: 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.springfra

我试图在jar中为一个方法设置切点,但它并没有被正确触发

我的rest端点代码如下所示:

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");}
}
注意:Point cut适用于操作类,其中as Point cut不适用于
org.apache.logging.log4j.LogManager
也尝试在Point cut中提供
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手册。它会告诉你,不是给非弹簧POJO。为此,您需要完整的AspectJ,可以在Spring中使用,也可以完全不使用Spring


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

您能否提供有关需要在此处进行更改以实现我的输出的代码。这不是代码,而是配置。您是否关注了我关于AspectJ LTW的链接?我尝试提供
@EnableLoadTimeWeaving
,但没有成功。如果你能提供我一个工作代码将更有帮助。尝试了该网站中的所有内容。启动JVM时需要
-javaagent:/path/to/aspectjweaver.jar
@enableloadtimewaving
只能与一些相当棘手的容器配置结合使用,Java代理是最安全的选择。您还需要将aop.xml放在正确的位置,但这都是手册中写的。我重复一遍:这不是关于更改代码,而是关于更改配置。您没有在这里共享您的配置或JVM命令行,所以我如何告诉您具体要更改什么?你也需要自己做一些工作。如果你在GitHub上共享一个完整的incl.Maven POM,我可以看一下。但是,请先自己尝试一下,不要把我当作不完全阅读文档的捷径。
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}