具體異常信息如下:
1 Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'hello' must be of type [com.hyq.chapter08_04_3.HelloImpl], but was actually of type [com.sun.proxy.$Proxy13] 2 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:376) 3 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) 4 at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:979) 5 at com.hyq.chapter08_04_3.AspectTest.main(AspectTest.java:10)
我在網上搜了一大堆解決方法都不管用,后來發現只要在spring的配置文件bean.xml中的<aop:aspectj-autoproxy/>加上一個屬性 proxy-target-class="true",即為:
<aop:aspectj-autoproxy proxy-target-class="true"/>
網上查了一下proxy-target-class="true"的相關作用如下:
- proxy-target-class屬性值決定是基於接口的還是基於類的代理被創建。
- 如果proxy-target-class 屬性值被設置為true,那么基於類的代理將起作用(這時需要cglib庫)。
- 如果proxy-target-class屬值被設置為false或者這個屬性被省略,那么標准的JDK 基於接口的代理將起作用。