Spring aop is not getting triggerd for external jar method in spring boot application

大憨熊 提交于 2020-05-17 08:04:18

问题


I am trying to have point cut for a method in jar and it is not getting triggered properly

I have my rest endpoint code as below :

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

Operation class :

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

Note : Point cut is working for Operation class where as point cut is not working for org.apache.logging.log4j.LogManager tried also providing org.apache.logging.log4j.Logger in point cut.

I expect the output as :

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

but the actual output is :

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}

回答1:


This question is a "classic" and has been asked so many times here already...

Please read the Spring AOP manual before using a tool you don't know. It will tell you that Spring AOP can only be applied to Spring components/beans, not to non-Spring POJOs. For that you need full AspectJ which you can use within Spring or completely without Spring.

Log4J classes are not Spring components, so the above applies to your situation. Here you find information about how to use AspectJ load-time weaving (LTW) instead of Spring AOP for your purpose.



来源:https://stackoverflow.com/questions/58074822/spring-aop-is-not-getting-triggerd-for-external-jar-method-in-spring-boot-applic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!