<? 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" > </ bean > < bean id ="database" class ="research.spring.beanfactory.ch3.Database" > </ bean > </ beans >
DAO depend-on Database时,也可以在DAO上定义setDatabase方法来接收一个Database的实例。这样Sping会保证DAO创建前先创建Database实例,然后在把实例化DAO后调用DAO的setDatabase方法把刚才创建的Database的实例注入给DAO。前提条件时Database必须定义成单例的。否则Spring在DAO depend-on Database时会创建一个Database的实例,在DAO.setDatabase时又会创建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 >
<? 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我们可以管理那些依赖关系不明显或者没有直接依赖关系的对象。<script language="JavaScript" src="/Utility/sendf.js" type="text/javascript"></script>