http://zhaohe162.blog.163.com/blog/static/3821679720111171219156/
Spring内核研究-管理bean之间的关系一(depends-on)
depend-on用来表示一个Bean的实例化依靠另一个Bean先实例化。如果在一个bean A上定义了depend-on B那么就表示:A 实例化前先实例化 B。在这种情况下你可以使用depends-on来定义在DAO被实例化之前先去实例化Database。你可这样定义:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean name="dao" class="research.spring.beanfactory.ch3.Dao" depends-on="database" > </bean> <bean id="database" class="research.spring.beanfactory.ch3.Database"> </bean> </beans>通过定义depends-on=”database”可以控制Sping实例化dao的顺序。在任何时候Spring总会保证实例化DAO之前先实例Database。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="dao" class="research.spring.beanfactory.ch3.Dao" depends-on="database "> <property name="database"> <ref bean="database"></ref> </property> </bean> <bean id="database" class="research.spring.beanfactory.ch3.Database"> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="dao" class="research.spring.beanfactory.ch3.Dao" depends-on="database"> <constructor-arg> <ref bean="database"></ref> </constructor-arg> </bean> <bean id="database" class="research.spring.beanfactory.ch3.Database"> </bean> </beans>
Spring允许Bean和Bean依赖的Bean(合作者)上同时定义depends-on。比如A depends-on B && B depends-on C && C depends-on D。下面这样定义是合法的。Sping实例化他们的顺序是D->C->B->A。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="a" class="research.spring.beanfactory.ch3.A" depends-on="b" /> <bean name="b" class="research.spring.beanfactory.ch3.B" depends-on="c" /> <bean name="c" class="research.spring.beanfactory.ch3.C" depends-on="D" /> <bean name="d" class="research.spring.beanfactory.ch3.D" /> </beans>
一个Bean可以同时depends-on多个对象如,A depends-on D,C,B。可以使用“,”或“;”定义多个depends-on的对象。<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="a" class="research.spring.beanfactory.ch3.A" depends-on="b" /> <bean name="b" class="research.spring.beanfactory.ch3.B" depends-on="c" /> <bean name="c" class="research.spring.beanfactory.ch3.C" depends-on="D" /> <bean name="d" class="research.spring.beanfactory.ch3.D" depends-on="A" /> </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="a" class="research.spring.beanfactory.ch3.A" depends-on="d,c,b" /> <bean name="b" class="research.spring.beanfactory.ch3.B" /> <bean name="c" class="research.spring.beanfactory.ch3.C" /> <bean name="d" class="research.spring.beanfactory.ch3.D" /> </beans>
上面的例子中A的实例化需要先实例化D,C,B。Spring会按照depend-on中定义的顺序来处理Bean。在这个例子里Spring实例化对象 的顺利是D->C->B->A。虽然实例化对象的顺序和前面“A depends-on B && B depends-on C && C depends-on D”的情况一下,但是这里的意义是完全不同的。不能用“A depends-on D,C,B”代替“A depends-on B && B depends-on C && C depends-on D”。
depends-on是一个非常又用的功能,借助depends-on我们可以管理那些依赖关系不明显或者没有直接依赖关系的对象。