1. 程式人生 > >Spring-depends-on

Spring-depends-on

trigger pre enc XML ger amp anon nat bean

如果沒有一個Bean依賴另一個Bean的static代碼塊進行初始化的話,我們可以使用depend-on實現,官方文檔如下:

For most situations, the fact that a bean is a dependency of another is expressed simply by the fact that one bean is set as a property of another. This is typically done with the ref element in the XmlBeanFactory. In a variation of this, sometimes a bean which is aware of the container is simply given the id of its dependency (using a string value or alternately the idref

element, which evaluates the same as a string value). The first bean then programmatically asks the container for its dependency. In either case, the dependency is properly initialized before the dependent bean.

For the relatively infrequent situations where dependencies between beans are less direct (for example, when a static initializer in a class needs to be triggered, such as database driver registration), the depends-on

element may be used to explicitly force one or more beans to be initialized before the bean using this element is initialized.

Following is an example configuration:

<bean id="beanOne" class="ExampleBean" depends-on="manager">
    <property name="manager"><ref local="manager"/></property>
</bean>

<bean id="manager" class="ManagerBean"/>

Spring-depends-on