ProxyFactoryBean 使用 JdkDynamicAopProxy 而不是 CGLIB

ProxyFactoryBean uses JdkDynamicAopProxy instead of CGLIB

我正在尝试为 SimpleDateFormat 对象配置 ProxyFactoryBeanThreadLocalTargetSource。 如图所示

Spring AOP 未使用 CGLIB 代理 SimpleDateFormat,因此当它尝试验证与 SimpleDateFormat 的兼容性时 returns ConversionNotSupportedException

我不明白它在做什么。几个小时以来我一直在调试,但我无法理解。 你们有什么感想?我究竟做错了什么?我缺少依赖项吗?

Spring @Configuration 如下完成

@Bean("yyyyMMdd")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public DateFormat simpleDateFormatYyyyMmDd() {
    return new SimpleDateFormat("yyyyMMdd");
}

@Bean(destroyMethod = "destroy")
public ThreadLocalTargetSource threadLocalYyyyMmDd() {
    final ThreadLocalTargetSource threadLocalTargetSource = new ThreadLocalTargetSource();
    threadLocalTargetSource.setTargetBeanName("yyyyMMdd");
    return threadLocalTargetSource;
}

@Bean
@Primary
public ProxyFactoryBean proxiedThreadLocalTargetSource(final ThreadLocalTargetSource threadLocalTargetSource) {
    final ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
    proxyFactoryBean.setTargetSource(threadLocalTargetSource);
    return proxyFactoryBean;
}

为以后自己解答,以防万一:

阅读此 Spring 文档段落 https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-introduction-proxies

如@SotiriosDelimanolis 所写,要强制使用 CGLIB,需要在 ProxyFactoryBean 对象上调用 setProxyTargetClass(true)