Spring Bean 作用域和生命周期

原文链接:https://juejin.cn/post/7105038058675765262

Spring Bean 作用域和生命周期

一、Spring Bean 作用域

常规的 Spring IoC 容器中Bean的作用域有两种:singleton(单例)和prototype(非单例)

注:基于Web的容器还有其他种作用域,在这就不赘述了。

1 singleton(单例)

案例1

  1. 创建Dept类
public class Dept {
    //部门编号
    private int deptNo;
    //部门名称
    private String deptName;
}
复制代码
  1. 编写Spring配置文件,并将scope 属性设置为singleton


复制代码
  1. 编写运行程序
public static void main(String[] args) {
    //获取IoC容器
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    //从容器中获取对象
    Dept dept1 = context.getBean("dept", Dept.class);
    Dept dept2 = context.getBean("dept", Dept.class);
    //打印对象
    System.out.println(dept1);
    System.out.println(dept2);
}
复制代码
  1. 结果如下,可以发现打印出的是同一个对象
Spring Bean 作用域和生命周期

2 prototype(原型)

案例2

  1. 只需修改scope 属性为prototype,其他代码不变。


复制代码
  1. 运行结果如下
Spring Bean 作用域和生命周期

3 小结

spring bean默认为单例,避免了对象的频繁创建与销毁,达到了bean对象的复用,性能高。

像表现层、业务层、数据层、工具类对象只需要调用方法,比较适合交给Spring IoC容器管理

但是像那种需要封装实例的域对象,因为会引发线程安全问题,不适合交给Spring IoC容器管理。

二、Spring Bean生命周期

Spring Bean生命周期:Spring Bean 对象从创建到销毁的整体过程。

Spring Bean生命周期大致可以分为以下 5 个阶段:1.Bean 的实例化、2.Bean 属性赋值、3.Bean 的初始化、4.Bean 的使用、5.Bean 的销毁

Spring 根据 Bean 的作用域来选择 Bean 的管理方式。

综上所述: 为了更好研究如何控制bean周期,下面案例中创建的bean默认都使用单例模式。

0 如何关闭容器

由于ApplicationContext类中没有关闭容器的方法,所以想要关闭容器需要用到ApplicationContext的子类——ClassPathXmlApplicationContext类。该类又有两种方法可以关闭容器

1、close关闭容器

//获取 ClassPathXmlApplicationContext 容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//调用close方法关闭容器
context.close();
复制代码

2、注册钩子关闭容器

//获取 ClassPathXmlApplicationContext 容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//调用注册狗子关闭容器
context.registerShutdownHook();
复制代码

1 生命周期回调

Bean 生命周期回调的方法主要有两种:

我们可以通过以下方式 2种方式自定义 Bean 的生命周期回调方法:

2 通过接口设置生命周期

我们可以在 Spring Bean 的 Java 类中,通过实现 InitializingBean 和 DisposableBean 接口,指定 Bean 的生命周期回调方法。

案例1

  1. 创建User类,并实现InitializingBean, DisposableBean接口,重写afterPropertiesSet()和destroy()方法。代码如下
/**
 * 继承接口,程序初始化回调和销毁回调方法
 */
public class User implements InitializingBean, DisposableBean {
    String name;
    int age;

    //setter方法
    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //初始化回调方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("这是初始化回调方法");
    }

    //销毁回调方法
    @Override
    public void destroy() throws Exception {
        System.out.println("这是销毁回调方法");
    }

    //toString方法
    @Override
    public String toString() {
        return "User{" +
            "name='" + name + ''' +
            ", age=" + age +
            '}';
    }
}
复制代码
  1. 编写spring配置文件

    
    

复制代码
  1. 编写运行程序
public class App {
    public static void main(String[] args) {
        //获取 ClassPathXmlApplicationContext 容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取对象
        User user = context.getBean("user", User.class);
        //使用bean
        System.out.println(user);
        //调用close方法关闭容器
        context.close();
    }
}
复制代码
  1. 运行结果如下
Spring Bean 作用域和生命周期

3 通过xml设置生命周期

注意:由于通过接口设置生命周期的方式会导致代码的耦合性过高,所以通常情况下,我们会通过xml设置生命周期。

通过 元素中的 init-method 和 destory-method 属性,指定 Bean 的生命周期回调方法。

案例2

  1. 创建User类,这次不需要继承那两个接口了,但要在添加两个普通方法(方法名可任意):init()和destory()代表初始化和销毁方法。代码如下
/**
 * 通过XML配置指定回调方法
 */
public class User {
    String name;
    int age;

    //setter方法
    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //初始化回调方法
    public void init() throws Exception {
        System.out.println("这是初始化回调方法");
    }

    //销毁回调方法
    public void destroy() throws Exception {
        System.out.println("这是销毁回调方法");
    }

    //toString方法
    @Override
    public String toString() {
        return "User{" +
            "name='" + name + ''' +
            ", age=" + age +
            '}';
    }
}
复制代码
  1. 编写spring配置文件,在元素里添加init-method和destroy-method属性,并指定User类中自定义的init和destory方法(关键)


    
    

复制代码
  1. 运行程序和运行结果都与案例1相同,这里就少些笔墨介绍了
展开阅读全文

页面更新:2024-03-19

标签:生命周期   作用   初始化   容器   属性   接口   对象   案例   代码   方法

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top