gpt4 book ai didi

Spring AOP : Capture inner private method calls (@EnableAspectJAutoProxy)

转载 作者:行者123 更新时间:2023-12-05 07:28:35 27 4
gpt4 key购买 nike

我需要捕获私有(private)内部调用方法。

因此,我需要将 aspectj 编织到我的 spring-boot 项目中:

@Configuration
@EnableAspectJAutoProxy
public class ApiConfiguration implements WebMvcConfigurer { /*...*/ }

我需要捕获一个@Service private 方法执行:

package net.space.service;

// imports

@Service
public class RepositoryService {
private void privateMethod(String param) {
/* Do something */
}

public void innerCaller() {
this.privateMethod(null);
}
}

重要:privateMethod私有(private)的,仅由innerCaller 调用。

然而,建议从未达成。我该如何解决?

我也试过这个切入点:

@Pointcut(value = "execution(* privateMethod(..))")
public void privatePointcut() {
}

和建议:

@AfterReturning("privatePointcut()")
public void groupMetrics(JoinPoint point) throws Throwable {
// Do something
}

我也尝试过:

@Pointcut(value = "execution(* net.space.service.RepositoryService.privateMethod(..))")
@Pointcut(value = "execution(* RepositoryService.privateMethod(..))")

编辑

我也试过使用@EnableLoadTimeWeaving:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver' defined in class path resource [org/springframework/context/annotation/LoadTimeWeavingConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.instrument.classloading.LoadTimeWeaver]: Factory method 'loadTimeWeaver' threw exception; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]

最佳答案

我得到了答案;

Spring AOP 是基于代理的,但对非公共(public)方法有限制;

Due to the proxy-based nature of Spring’s AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn’t applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!

If your interception needs include protected/private methods or even constructors, consider the use of Spring-driven native AspectJ weaving instead of Spring’s proxy-based AOP framework. This constitutes a different mode of AOP usage with different characteristics, so be sure to make yourself familiar with weaving first before making a decision.

因此您需要启用 native AspectJ weaving与以下;

The example presented here uses XML style configuration, it is also possible to configure and use @AspectJ with Java Configuration. Specifically the @EnableLoadTimeWeaving annotation can be used as an alternative to (see below for details).

那么你可以试试;

@Configuration
@EnableLoadTimeWeaving
public class ApiConfiguration implements WebMvcConfigurer { /*...*/ }

关于 Spring AOP : Capture inner private method calls (@EnableAspectJAutoProxy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53298940/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com