1、一个bean对象依赖另一个bean依赖项,需要xml的ref来实现他们的依赖关系,也可以用depends-on来实现依赖关系。
2、要表示对多个bean的依赖关系,提供一个bean名称列表作为依赖属性的值(逗号、空格和分号是有效的分隔符):
3、依赖属性既可以指定初始化时依赖关系,也可以指定对应的销毁时依赖关系(仅在单例bean中)。因此,依赖还可以控制关闭顺序。
4、实例用ref来实现依赖关系:
(1) BeanOne 对象单独不依赖其他对象。
package com.it.app.bean;
import org.springframework.beans.factory.DisposableBean;
public class BeanOne implements DisposableBean {
public BeanOne(){
System.out.println("---BeanOne对象创建");
}
@Override
public void destroy() throws Exception {
System.out.println("---BeanOne对象正在销毁");
}
}
(2)BeanTwo对象依赖于beanOne。
package com.it.app.bean;
import org.springframework.beans.factory.DisposableBean;
public class BeanTwo implements DisposableBean {
private BeanOne beanOne;
public BeanTwo(BeanOne beanOne){
this.beanOne = beanOne;
System.out.println("---BeanTwo对象创建");
}
@Override
public void destroy() throws Exception {
System.out.println("---BeanTwo对象正在销毁");
}
}
(3)BeanThree对象依赖BeanTwo
package com.it.app.bean;
import org.springframework.beans.factory.DisposableBean;
public class BeanThree implements DisposableBean {
private BeanTwo beanTwo;
public BeanThree(BeanTwo beanTwo){
this.beanTwo = beanTwo;
System.out.println("---BeanThree对象创建");
}
@Override
public void destroy() throws Exception {
System.out.println("---BeanThree对象正在销毁");
}
}
(4)xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanOne" class="com.it.app.bean.BeanOne"></bean>
<bean id="beanTwo" class="com.it.app.bean.BeanTwo">
<constructor-arg ref="beanOne"></constructor-arg>
</bean>
<bean id="beanThree" class="com.it.app.bean.BeanThree">
<constructor-arg ref="beanTwo"></constructor-arg>
</bean>
</beans>
(5)容器启动初始化和关闭执行顺序:
package com.it.app;
import com.it.app.service.ServiceImpl4;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppTwo {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-contextTwo.xml");
System.out.println("****spring容器已启动****");
context.close();
System.out.println("****spring容器已关闭****");
}
}
(6) 依赖初始化和关闭执行顺序:
初始化:BeanOne–>BeanTwo–>BeanThree
关闭: BeanThree -->BeanTwo–>BeanOne
5、实例用depends-on来实现依赖关系:
(1) beanOne ,beanTwo,beanThree对象创建:
package com.it.app.bean;
import org.springframework.beans.factory.DisposableBean;
public class BeanOne implements DisposableBean {
public BeanOne(){
System.out.println("---BeanOne对象创建");
}
@Override
public void destroy() throws Exception {
System.out.println("---BeanOne对象正在销毁");
}
}
package com.it.app.bean;
import org.springframework.beans.factory.DisposableBean;
public class BeanTwo implements DisposableBean {
public BeanTwo(){
System.out.println("---BeanTwo对象创建");
}
@Override
public void destroy() throws Exception {
System.out.println("---BeanTwo对象正在销毁");
}
}
package com.it.app.bean;
import org.springframework.beans.factory.DisposableBean;
public class BeanThree implements DisposableBean {
public BeanThree(){
System.out.println("---BeanThree对象创建");
}
@Override
public void destroy() throws Exception {
System.out.println("---BeanThree对象正在销毁");
}
}
xml信息,依赖关系:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- <bean id="beanOne" class="com.it.app.bean.BeanOne"></bean>
<bean id="beanTwo" class="com.it.app.bean.BeanTwo">
<constructor-arg ref="beanOne"></constructor-arg>
</bean>
<bean id="beanThree" class="com.it.app.bean.BeanThree">
<constructor-arg ref="beanTwo"></constructor-arg>
</bean>-->
<bean id="beanThree" class="com.it.app.bean.BeanThree" depends-on="beanTwo,beanOne"></bean>
<bean id="beanTwo" class="com.it.app.bean.BeanTwo"></bean>
<bean id="beanOne" class="com.it.app.bean.BeanOne" ></bean>
</beans>
(2) 依赖初始化和关闭执行顺序:
初始化:BeanOne–>BeanTwo–>BeanThree
关闭: BeanOne -->BeanTwo–>BeanThree