spring aop 配置class的异常

在使用spring进行ioc配置aop的时候,我们通常是对接口进行编程的,属性的注入是接口。但如果由于某些原因,我们需要将class注入的时候,我们就会得到一个异常:

java 代码
  1. PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Fail   
  2. ed to convert property value of type [$Proxy7] to required type [org.appfuse.net   
  3. bar.service.impl.NetBarManagerImpl] for property 'netBarManager'; nested excepti   
  4. on is java.lang.IllegalArgumentException: No matching editors or conversion stra   
  5. tegy found  

 

查阅一下资料发现,这是由于java的AOP机制造成的。spring-reference有如下说明:

Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK
dynamic proxies are preferred whenever you have a choice).

由于jdk dynamic proxies是对接口进行的,因此无法对实体类进行正常的proxy。解决方法配置proxyTargetClass="true",可以参见下面的链接:

http://www.cwinters.com/news/display/3410

对于spring2的aop配置如下:

<aop:config proxy-target-class="true">
xml 代码
  1. <aop:config proxy-target-class="true">  
  2. <!-- other beans defined here... -->  
  3. </aop:config>  
</aop:config>

你可能感兴趣的:(spring,AOP,jdk,编程,Appfuse)