How to Log all the methods public method calls in

2019-09-15 00:57发布

I have some issue in Logging all the methods when a service calls. Code is like this:

package com.myproject.controller;
@RestController(/person)
public class Controller{
  public Person getpersonInfo(){
     ......
     getValidPerson();
  }
}

public Person getValidPerson() {

    isPersonValid(Person person);
    ....
}

Person class Methods :

package com.myproject.dao;
public class Dao{
   public boolean isPersonValid(){
     //Checks for the person is Valid
   }
}

Aspect Class :

package com.myproject;
@Component
@Aspect
public class Logging{

  @Before("execution(* com.myproject..*.*(..)))")
  public void beforeServiceCall(Jointpoint jp) {
    //Some Logging function
  }

}

Main class like this

package com.myproject;
@SpringBootApplication
@EnableAutoConfiguration
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
@EnableAspectJAutoProxy()
public class Main implements LoadTimeWeavingConfigurer{
  public static void main(String[] args){
  ......
  }
}

Pom file :

       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.1</version>
        </dependency>

When I call the service http://localhost:8080/person - GET getpersonInfo() is only Logged in this case, I have also tried LTW , but do not solve

I need to log all internal methods to the service like is getValidPerson(),isPersonValid() mentioning all arguments to those invoked method.

1条回答
老娘就宠你
2楼-- · 2019-09-15 00:59

The classical case, answered here about 378 times before:

  • Spring AOP is proxy-based and thus cannot intercept method calls like this.someMethod() (equivalent to just someMethod()) because this is the real object, not the proxy. No proxy usage means no Spring AOP interception. This is well-documented. Look for the term "self-invocation" in the Spring AOP manual.
  • If you want to intercept internal method calls, use AspectJ with LTW, not Spring AOP. It does not need/use proxies and will just work if configured correctly. Chapter 11.8 in the Spring manual tells you how to do that.

Some remarks:

  • You really should read the manual before using a tool.
  • You should search for existing questions before creating new ones on StackOverflow.
  • Initially you asked about how to intercept public and private methods. Then you edited the subject and the question itself and it became clear that it was not about public vs private but about self-invocation. This is a completely different topic. I find it not so nice to do that, actually.
  • Maybe you want to upgrade your aspectjweaver dependency from 1.8.1 to 1.8.10 which contains lots of bugfixes and small improvements with regard to Java 8.
  • You do not need aspectjrt because it is a subset of aspectjweaver. The former is useful for compile-time weaving, not for load-time weaving.
查看更多
登录 后发表回答