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

Gau*_*eel 7 java spring spring-aop spring-boot

代理设置似乎在 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

Awe*_*ter 2

这是一个已知问题。

目前只能设置

spring.aop.proxy-target-class=false
Run Code Online (Sandbox Code Playgroud)

在您的配置文件中强制它使用 JDKProxy。