今天在做Spring AOP練習的時候,用經典方法。
創建代理對象的時候直接使用類路徑,使用這樣的方式
<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" value="test.spring.aop.bean.Human" />
<property name="interceptorNames" value="sleepHelperAdvisor" />
<property name="proxyInterfaces" value="test.spring.aop.bean.Sleepable" />
</bean>
運行就報錯了 nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
后來把 human手動初始化,再關聯進來。
<bean id="human" class="test.spring.aop.bean.Human" />
<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="human" />
<property name="interceptorNames" value="sleepHelperAdvisor" />
<property name="proxyInterfaces" value="test.spring.aop.bean.Sleepable" />
</bean>
這樣就不報錯了,說明需要先聲明才行。奇怪為什么下面那個接口可以直接用,不用聲明。我就試着聲明接口之后再ref他。
結果就報錯了 nested exception is org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [test.spring.aop.bean.Sleepable]: Specified class is an interface
不能實例化接口。