Bean的作用域和生命周期

本文详细介绍了Spring中Bean的作用域,包括单例(singleton)和原型(prototype)的区别,以及如何设置它们。同时,文章还深入讲解了Bean的生命周期,从创建到销毁的各个阶段,包括构造器、setter方法、初始化和销毁方法的调用,以及BeanPostProcessor的使用。通过一个Order类的示例展示了Bean的完整生命周期过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Bean的作用域

1. 在Spring里面,设置创建的bean实例是单实例还是多实例?

public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = context.getBean("user",User.class);
        User user1 = context.getBean("user",User.class);
        System.out.println(user);
        System.out.println(user1);
    }
  • 测试结果
    在这里插入图片描述

分析结果发现我们获取的bean是相同的,默认为单实例对象

2. 如何设置单实例还是多实例?

  • 在spring配置文件bean标签里面有属性scope用于设置单实例还是多实例
  • scope属性值
  1. 默认值,singleton,表示单实例对象
    2.prototype,表示是多实例对象
<bean id="user" class="com.github.entity.User" scope="prototype">
        <property name="name" value="zhangsan"></property>
        <property name="age" value="15"></property>
        <property name="gender" value="男"></property>
    </bean>

在这里插入图片描述

分析结果发现获取的bean不相同

3. singleton和prototype区别

  • singleton单实例,prototype为多实例
  • 设置scope值是singleton时候,加载spring配置文件时候就会创建单实例对象
  • 设置scope值是prototype时候,不是在加载spring配置文件的时候创建对象,在调用getBean方法时候创建多实例对象

二、Bean生命周期

1.生命周期

  • 从对象创建到对象销毁的过程

2. bean生命周期

  • 通过构造器创建bean实例(无参数构造)
  • 为bean的属性设置值和对其他bean引用(调用set方法)
  • 把bean实例传递给bean前置处理器的方法postProcessBeforeInitialization
  • 调用bean的初始化方法
  • 把bean实例传递给bean后置处理器的方法postProcessAfterInitialization
  • 获取bean实例对象
  • 当容器关闭时候,调用bean的销毁方法

3.演示bean生命周期

  • 创建Order类
public class Order {
    String name;

    public Order() {
        System.out.println("第一步:调用无参构造方法");
    }


    public void setName(String name) {
        this.name = name;
        System.out.println("第二步:调用set方法");
    }

    public void add(){
        System.out.println("第四步:调用初始化方法");
    }
    public void sub(){
        System.out.println("第七步:调用销毁方法");
    }
}
  • 实现BeanPostProcessor接口
public class MybeanPost implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("第三步:调用before方法");
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("第五步:调用After方法");
        return null;
    }
}
  • 创建配置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="order" class="com.github.entity.Order" init-method="add" destroy-method="sub">
        <property name="name" value="订单"></property>
    </bean>
    <bean id="mybeanPost" class="com.github.entity.MybeanPost"></bean>

</beans>
  • 编写测试类
public class OrderTest {


    @Test
    public void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
        Order order = context.getBean("order",Order.class);
        System.out.println("第六步:获取实例对象");
        context.close();
    }
}

  • 运行结果如图
    在这里插入图片描述
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值