在Spring Boot应用程序中,没有为外部JAR方法触发Spring AOP [英] Spring aop is not getting triggerd for external jar method in spring boot application

查看:0
本文介绍了在Spring Boot应用程序中,没有为外部JAR方法触发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.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;
    }
}

注意: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来实现您的目的的信息。

这篇关于在Spring Boot应用程序中,没有为外部JAR方法触发Spring AOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆