有時(shí)候,在SPRING中兩個(gè)類互相含有對(duì)方的聲明,一般情況這是不允許并且極可能是有錯(cuò)誤的。
會(huì)報(bào)這個(gè)錯(cuò):
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘***’: Bean with name ‘***’ has been injected into other beans [***, ***]in its raw version as part of a circular reference,
but has eventually been wrapped (for example as part of auto-proxy creation). This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using ‘getBeanNamesOfType’ with the ‘allowEagerInit’ flag turned off, for example.
但有時(shí)候這正是我們想要的,考慮這種情況:
我們用一個(gè)ManagerFactory來(lái)包含所有Manager的聲明,以便在程序中用到多個(gè)Manager的地方,一個(gè)入口集中訪問(wèn)。但是,可能某個(gè)Manager本身就需要用到其它幾個(gè)Manager,進(jìn)而用到ManagerFactory。這時(shí),我們可以這樣聲明即可:
<bean id="managerFactory" class="common.utils.ManagerFactory" lazy-init="true"/>