BeanNameAutoProxyCreator支持拦截接口和类,但不支持已经被jdk代理过的类$Proxy8。使用cglib才能代理,如下

<!-- 通过bean的名字来匹配选择要代理的bean,在使用时仍用原有bean的id从context中获取 -->
<bean id="autoProxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 需要代理的service,以userService结尾注册的bean -->
<property name="beanNames">
<list>
<value>*userService</value>
<value>*routeService</value>
</list>
</property>
<!-- 拦截器的名称 -->
<property name="interceptorNames">
<list>
<value>validateUserAdvisor</value>
<!-- 性能监视增强 -->
<value>performanceMonitorAdvice</value>
</list>
</property>
<!-- 设置强制使用CGLIB生成代理,此时代理的bean如果已经是代理则必须也是由cglib生成的 -->
<property name="optimize" value="true" />
</bean>

optimize是ProxyConfig的属性。意思为 是否对生产代理策略使用优化。

public class ProxyConfig implements Serializable {
private boolean proxyTargetClass = false;
private boolean optimize = false;
boolean opaque = false;
boolean exposeProxy = false;
private boolean frozen = false;
}

一个例子

  Spring AOP 提供一个可根据Bean名称来自动生产代理的工具,它就是BeanNameAutoProxyCreator。它的配置是这样:

    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*impl"></property> <!-- 只为后缀为"impl"的bean生产代理 -->
<property name="interceptorNames" value="aServiceAdvisor"></property> <!-- 一个增强 -->
<property name="optimize" value="true"></property> <!-- 是否对代理策略进行优化 -->
</bean>

  以上使用BeanNameAutoProxyCreator 只为后缀为"impl"的Bean生产代理。需要注意的是,这个地方我们不能定义代理接口,也就是interfaces属性,因为我们根本就不知道这些Bean到低实现了多少接口。此时不能代理接口,而只能代理类。所以这里提供了一个新的配置项,它就是optimize。如果为true,则可以对代理生成策略进行优化(默认为false)。也就是说,如果该类有接口,就代理接口(使用JDK代理);如果没有接口,就代理类(使用CGLIB代理)。并非像之前的使用的proxyTargetClass 属性那样,强制代理类,而不去考虑代理接口的方式。

  那么就有一个问题了,既然有了CGLIB 可以代理任何类了,那为什么还要JDK的动态代理了?

  是因为CGLIB 创建有个特点(创建慢,执行块),然而JDK刚好相反(创建快,执行慢)。如果在运行的时候不断的用CGLIB去创建代理,系统的性能会大打折扣,所以建议一般在系统初始化的使用用CGLIB去创建代理,并放入spring的ApplicationContext中以备后用。

流程分析

    @Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}

ProxyTargetClass (是否强制使用CGLIB来实现代理)

(true : 强制使用CGLIB来实现代理)

(false : 不强制使用CGLIB来实现代理,首选JDK来实现代理)(默认值)

isOptimize (是否对生成代理策略进行优化)

(true :  进行优化,如果有接口就代理接口(使用JDK动态代理),没有接口代理类(CGLIB代理))

(false : 不进行优化) (默认值)

Spring注入bean失败Failed to convert property value of type

Failed to convert property value of type [$Proxy13 
Failed to convert property value of type [$Proxy13] to required type

PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found 
Caused by: 
org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are: 
PropertyAccessException 1: 
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found 
Caused by: 
java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found 
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231) 
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138) 
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:815)

当系统出现类似上面的异常信息时,很可能你引用了已经实现了自动代理的Bean,  Cannot convert value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp' 提示的意思:你将authorDaoImp注入到一个spring受管Bean中,  但是spring容器提示你注入的Bean类型不对,它说你注入的是 [$Proxy13] 类型,这就说明你可能在无意中将authorDaoImp实现了代理, 此时你再在spring容器中引用 authorDaoImp时得到的将是代理类型。示例如下(在数据源,事务管理器配置完整的前提下):

http://grey2.iteye.com/blog/994926

A less preferable option is to specify proxy-target-class="true" to your transactional aspect (or whatever makes proxies around your objects)

https://stackoverflow.com/questions/5427077/failed-to-convert-property-value-of-type-proxy1-to-required-type

Spring AOP 代理类,BeanNameAutoProxyCreator cglib的更多相关文章

  1. jdk动态代理与cglib代理、spring aop代理实现原理

    原创声明:本博客来源与本人另一博客[http://blog.csdn.net/liaohaojian/article/details/63683317]原创作品,绝非他处摘取 代理(proxy)的定义 ...

  2. jdk动态代理与cglib代理、spring aop代理实现原理解析

    原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...

  3. 何为代理?jdk动态代理与cglib代理、spring Aop代理原理浅析

    原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...

  4. jdk动态代理与cglib代理、spring Aop代理原理-代理使用浅析

    原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...

  5. Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)

    Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误) 问题: 今天在用AfterReturningAdvice时,a ...

  6. Spring AOP 实现原理与 CGLIB 应用

    https://www.ibm.com/developerworks/cn/java/j-lo-springaopcglib/ AOP(Aspect Orient Programming),也就是面向 ...

  7. Spring AOP 实现原理与 CGLIB 应用--转

    AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理.安全检查.缓存.对象池管理等.AOP 实现的关键就在于 ...

  8. java中代理,静态代理,动态代理以及spring aop代理方式,实现原理统一汇总

    若代理类在程序运行前就已经存在,那么这种代理方式被成为 静态代理 ,这种情况下的代理类通常都是我们在Java代码中定义的. 通常情况下, 静态代理中的代理类和委托类会实现同一接口或是派生自相同的父类. ...

  9. 【转】Spring AOP 实现原理与 CGLIB 应用

    AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理.安全检查.缓存.对象池管理等.AOP 实现的关键就在于 ...

随机推荐

  1. SpringCloud 别人的主页

    https://www.cnblogs.com/xuwujing/ java(28) springBoot(14) 设计模式(14) springcloud(7) hadoop(7) hive(5) ...

  2. ******可用 SpringBoot 项目打包分开lib,配置和资源文件

    spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...

  3. Windows 10 更新补丁后Visual Studio 2017 运行项目出现错误

    问题: 今天更新了Windows 10(版本 1709)推送最新补丁后,打开Visual Studio 2017运行Web项目,都出现“指定的参数超出有效值的范围 参数名:site”,如下图: 解决方 ...

  4. win10家庭版添加本地策略

      在桌面新建一个空文件夹(此处是需要新建文件夹而并非文件),文件夹的名称大家随意即可.如下图所示:   打开刚刚新建的文件夹(双击文件夹则打开文件夹).如下图所示:   在刚刚我们打开的文件夹资源地 ...

  5. C# 常用工具方法之DataTable(一)

    1.DataTable 转 泛型T的List /// <summary> /// 数据集DataTable转换成List集合 /// </summary> /// <ty ...

  6. APS.NET MVC + EF (11)---过滤器

    过滤器本质就是对动作方法的执行过程进行干预,这种干预可以影响动作方法执行的各个过程.ASP.NET MVC 提供了4种类型的接口,并在接口中定义了各种成员,代表代码执行的各个阶段,这些接口和成员如表1 ...

  7. Git远程协作和分支

    一.远程基本操作 基本的配置远程仓库有两个命令: git remote add origin git@github.com:ZXZxin/gitlearn.git : git push -u orig ...

  8. Git分支和版本回退

    一.分支 1.分支简单介绍 简单使用: 可以将git branch new_branch和git checkout new_branch两个命令合并成一个命令: git checkout -b new ...

  9. 如何修改被readonly修饰的属性

    结论: 1.用KVC改变只读属性的值: 2.若禁止KVC方式修改只读属性的值,可在对应类重写类方法 // 该方法默认返回YES. 即在不存在满足条件的存取方法时,允许直接访问属性对应的实例变量+ (B ...

  10. 高性能TcpServer(Python) - SocketServer

    源码下载  -> 提取码  QQ:505645074 程序结构图  测试截图 1. 正常接收测试 2. 并发测试