在使用spring进行ioc配置aop的时候,我们通常是对接口进行编程的,属性的注入是接口。但如果由于某些原因,我们需要将class注入的时候,我们就会得到一个异常:
java 代码
- PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Fail
- ed to convert property value of type [$Proxy7] to required type [org.appfuse.net
- bar.service.impl.NetBarManagerImpl] for property 'netBarManager'; nested excepti
- on is java.lang.IllegalArgumentException: No matching editors or conversion stra
- 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 代码
- <aop:config proxy-target-class="true">
- <!-- other beans defined here... -->
- </aop:config>