Spring 未初始化聲明bean就ref,造成的異常


今天在做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

不能實例化接口。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM