在Spring Boot应用程序中,没有为外部JAR方法触发Spring AOP应用程序、方法、Boot、Spring

2023-09-03 14:34:38 作者:不珍惜我你就亏大了@

我正在尝试对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";
    }
}
开发您的第一个Spring Boot应用程序 版本Spring Boot 2.1.6.RELEASE

操作类:

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

注意:Point Cut适用于org.apache.logging.log4j.LogManager不适用于org.apache.logging.log4j.LogManager已尝试提供org.apache.logging.log4j.LoggerIn Point Cut的操作类。

我希望输出为:

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手册。它将告诉您Spring AOP can only be applied to Spring components/beans,而不是非Spring POJO。为此,您需要完整的AspectJ,您可以在Spring中使用它,也可以在没有Spring的情况下完全使用。

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