ProxyFactoryBean使用JdkDynamicAopProxy而不是CGLIB

时间:2018-07-02 16:47:17

标签: java spring spring-aop

我正在尝试为ProxyFactoryBean对象的ThreadLocalTargetSource配置一个SimpleDateFormat。 如您在图片中所见

Immagine.png

Spring AOP未使用CGLIB代理SimpleDateFormat,因此在尝试验证与ConversionNotSupportedException的兼容性时返回SimpleDateFormat

我不明白它在做什么。我几个小时以来一直在调试,我听不懂。 你们有什么感想?我究竟做错了什么?我是否缺少依赖项?

春天@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;
}

1 个答案:

答案 0 :(得分:0)

为将来的自己做准备,以防万一:

阅读此Spring文档段落what's the difference between redirect and dispatch in phalcon?

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