栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring的depends-on

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring的depends-on

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文件配置:





    
    
        
    
    
        
    

(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信息,依赖关系:






    
    
    

(2) 依赖初始化和关闭执行顺序:
初始化:BeanOne–>BeanTwo–>BeanThree
关闭: BeanOne -->BeanTwo–>BeanThree

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424351.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号