gpt4 book ai didi

java - Spring-Boot 2+ 强制使用 CGLIB 代理,即使 proxyTargetClass = false

转载 作者:行者123 更新时间:2023-12-03 11:18:18 42 4
gpt4 key购买 nike

代理设置似乎在 Spring-Boot 1.5+ 和 2.+ 之间发生了变化。

在带有@EnableAspectJAutoProxy(proxyTargetClass = false) 或只是@EnableAspectJAutoProxy 或什至没有注释@EnableAspectJAutoProxy 的Spring 1.5.20 中,我会得到一个JdkDynamicAopProxy。
使用 @EnableAspectJAutoProxy(proxyTargetClass = true) 我会得到 CGLIB 增强类。好的一切都好。

使用与 Spring 2.1.4 相同的代码,无论配置如何,我始终都会得到一个 CGLIB 增强的 ServiceImpl。

我没有设法在 Spring 2+ 中使用 JdkDynamicAopProxy 代理。

还有办法做到吗?

这是我的代码:

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = false)
public class DemoApplication {

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
MyService service = context.getBean(MyService.class);
service.execute();
}
}


@Aspect
@Component
public class ChronoAspect {
@Around("execution(* com.example.demo.service..*.*(..))")
public Object chronoAround(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
// .....
}

}

public interface MyService {
void execute();
}

@Component
public class ServiceImpl implements MyService {
public void execute() {
System.out.println("Hello execute from Service.execute");
}
}

最佳答案

这是一个已知问题。
目前您只能设置

spring.aop.proxy-target-class=false
在您的配置文件中强制它使用 JDKProxy。

关于java - Spring-Boot 2+ 强制使用 CGLIB 代理,即使 proxyTargetClass = false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853108/

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