5.1、bean的作用域

5.1.1、单例(默认且常用)

5.1.1.1、配置bean

注意:当bean不配置scope属性时,默认是singleton(单例)

<?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="student" class="org.rain.spring.pojo.Student"></bean> </beans>

5.1.1.2、测试

由控制台日志可知,此时ioc获取到的两个bean本质上是同一个对象

    @Test
public void testScope() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
Student student1 = applicationContext.getBean(Student.class);
Student student2 = applicationContext.getBean(Student.class);
System.out.println(student1 == student2);
}

5.1.2、多例

5.1.2.1、配置bean

<?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"> <!--
scope属性:设置bean的作用域
当属性值为singleton时,在IOC容器中这个bean的对象始终为单实例;且创建对象的时机是IOC容器初始化时
当属性值为prototype时,在IOC容器中这个bean的对象有多个实例;且创建对象的时机是获取bean时
-->
<bean id="student" class="org.rain.spring.pojo.Student" scope="prototype"></bean> </beans>

5.1.2.2、测试

由控制台日志可知,此时ioc获取到的两个bean本质上是不同的对象

5.1.3、其他作用域

如果是在WebApplicationContext环境下还会有另外两个作用域(但不常用):

  • request:在一个请求范围内有效

  • session:在一个会话范围内有效

5.2、bean的生命周期

5.2.1、创建User类

package org.rain.spring.pojo;

/**
* @author liaojy
* @date 2023/8/3 - 23:59
*/
public class User { private Integer id;
private String username;
private String password;
private Integer age; public User() {
System.out.println("生命周期1:创建对象");
} public User(Integer id, String username, String password, Integer age) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
} public Integer getId() {
return id;
} public void setId(Integer id) {
System.out.println("生命周期2:依赖注入");
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public void initMethod(){
System.out.println("生命周期3:初始化");
} public void destroyMethod(){
System.out.println("生命周期4:销毁");
} @Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}

5.2.2、配置bean

<?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"> <!--
init-method属性:指定初始化方法
destroy-method属性:指定销毁方法
-->
<bean id="user" class="org.rain.spring.pojo.User" init-method="initMethod" destroy-method="destroyMethod">
<property name="id" value="1"></property>
<property name="username" value="张三"></property>
<property name="password" value="123"></property>
<property name="age" value="11"></property>
</bean> </beans>

5.2.3、测试

    @Test
public void testLifecycle(){
//ConfigurableApplicationContext是ApplicationContext子接口,扩展了刷新和关闭容器的方法
ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
User user = ioc.getBean(User.class);
System.out.println(user);
ioc.close();
}

5.3、作用域对生命周期的影响

5.3.1、作用域为单例时

5.3.1.1、配置bean

    <bean id="user" class="org.rain.spring.pojo.User" init-method="initMethod" destroy-method="destroyMethod">
<property name="id" value="1"></property>
<property name="username" value="张三"></property>
<property name="password" value="123"></property>
<property name="age" value="11"></property>
</bean>

5.3.1.2、测试

由控制台日志可知,当bean的作用域为单例时,生命周期的前三个步骤会在获取IOC容器时执行

    @Test
public void testLifecycle(){
//ConfigurableApplicationContext是ApplicationContext子接口,扩展了刷新和关闭容器的方法
ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
}

5.3.2、作用域为多例时

5.3.2.1、配置bean

    <bean id="user" class="org.rain.spring.pojo.User" init-method="initMethod" destroy-method="destroyMethod" scope="prototype">
<property name="id" value="1"></property>
<property name="username" value="张三"></property>
<property name="password" value="123"></property>
<property name="age" value="11"></property>
</bean>

5.3.2.2、测试

由控制台日志可知,当bean的作用域为多例时,生命周期的前三个步骤会在获取bean时执行

    @Test
public void testLifecycle(){
//ConfigurableApplicationContext是ApplicationContext子接口,扩展了刷新和关闭容器的方法
ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
User user = ioc.getBean(User.class);
}

5.4、bean的后置处理器

5.4.1、创建bean的后置处理器

注意:自定义的bean后置处理器,需要实现BeanPostProcessor接口

package org.rain.spring.process;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; /**
* @author liaojy
* @date 2023/8/4 - 23:52
*/
public class MyBeanProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanProcessor-->后置处理器postProcessBeforeInitialization");
return bean;
} public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanProcessor-->后置处理器postProcessAfterInitialization");
return bean;
}
}

5.4.2、配置bean的后置处理器

注意:bean后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行

    <!-- bean的后置处理器要放入IOC容器才能生效 -->
<bean id="myBeanPostProcessor" class="org.rain.spring.process.MyBeanProcessor"></bean>

5.4.3、测试

由控制台日志可知,bean的后置处理器会在生命周期的初始化前后添加额外的操作

    @Test
public void testLifecycle(){
//ConfigurableApplicationContext是ApplicationContext子接口,扩展了刷新和关闭容器的方法
ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
User user = ioc.getBean(User.class);
System.out.println(user);
ioc.close();
}

5.5、具体的生命周期过程

  • bean对象创建(调用无参构造器)

  • 给bean对象设置属性

  • bean对象初始化之前操作(由bean的后置处理器负责)

  • bean对象初始化(需在配置bean时指定初始化方法)

  • bean对象初始化之后操作(由bean的后置处理器负责)

  • bean对象就绪可以使用

  • bean对象销毁(需在配置bean时指定销毁方法)

  • IOC容器关闭

5、Spring之bean的作用域和生命周期的更多相关文章

  1. 详解Spring中Bean的作用域与生命周期

    摘要:在利用Spring进行IOC配置时,关于bean的配置和使用一直都是比较重要的一部分,同时如何合理的使用和创建bean对象,也是小伙伴们在学习和使用Spring时需要注意的部分,所以这一篇文章我 ...

  2. Spring中Bean的作用域、生命周期

                                   Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...

  3. Spring之Bean的作用域与生命周期

    在前面博客中提到容器启动获得BeanDefinition对象中有一个scope 属性.该属性控制着bean对象的作用域.本章节介绍Bean的作用域及生命周期,了解bean是怎么来的又怎么没的. 一.B ...

  4. Spring中bean的作用域与生命周期

    在 Spring 中,那些组成应用程序的主体及由 Spring IOC 容器所管理的对象,被称之为 bean.简单地讲,bean 就是由 IOC 容器初始化.装配及管理的对象,除此之外,bean 就与 ...

  5. Spring中Bean的作用域和生命周期

    作用域的种类 Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域.Spring3 为 Bean 定义了五种作用域,具体如下. 1)singleton 单例模式,使用 sing ...

  6. Spring bean的作用域和生命周期

    bean的作用域 1.singleton,prototype, web环境下:request,session,gloab session 2.通过scope="" 来进行配置 3. ...

  7. Spring框架系列(三)--Bean的作用域和生命周期

    Bean的作用域 Spring应用中,对象实例都是在Container中,负责创建.装配.配置和管理生命周期(new到finalize()) Spring Container分为两种: 1.BeanF ...

  8. Spring bean的作用域以及生命周期

    一.request与session的区别 request简介 request范围较小一些,只是一个请求. request对象的生命周期是针对一个客户端(说确切点就是一个浏览器应用程序)的一次请求,当请 ...

  9. Spring ( 三 ) Spring的Bean的装配与生命周期、专用测试

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.对象的生命周期 1.IOC之Bean的生命周期 创建带有生命周期方法的bean public cla ...

  10. bean的作用域和生命周期

    一.Bean作用域 二.生命周期 其中,这个类实现各种接口重写各种方法,会按bean的声明周期按序执行: 其中,自定义的初始化和自定义销毁的方法不是实现接口重写,而是成员方法,并且在装配bean即在x ...

随机推荐

  1. 2022-03-30:有m个同样的苹果,认为苹果之间无差别, 有n个同样的盘子,认为盘子之间也无差别, 还有,比如5个苹果如果放进3个盘子, 那么1、3、1和1、1、3和3、1、1的放置方法,也认为是

    2022-03-30:有m个同样的苹果,认为苹果之间无差别, 有n个同样的盘子,认为盘子之间也无差别, 还有,比如5个苹果如果放进3个盘子, 那么1.3.1和1.1.3和3.1.1的放置方法,也认为是 ...

  2. 2021-11-13:至少有 K 个重复字符的最长子串。给你一个字符串 s 和一个整数 k ,请你找出 s 中的最长子串, 要求该子串中的每一字符出现次数都不少于 k 。返回这一子串的长度。提示:1

    2021-11-13:至少有 K 个重复字符的最长子串.给你一个字符串 s 和一个整数 k ,请你找出 s 中的最长子串, 要求该子串中的每一字符出现次数都不少于 k .返回这一子串的长度.提示:1 ...

  3. 2021-09-19:数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且有效的括号组合。

    2021-09-19:数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且有效的括号组合. 福大大 答案2021-09-19: 递归. 参数1:左括号-右括号的数量. 参数2:左 ...

  4. Pandas 加载数据的方法和技巧

    哈喽大家好,我是咸鱼 相信小伙伴们在学习 python 数据分析的过程中或多或少都会听说或者使用过 pandas pandas 是 python 的一个拓展库,常用于数据分析 今天咸鱼将介绍几个关于 ...

  5. 《数据结构》之栈和堆结构及JVM简析

    导言: 在数据结构中,我们第一了解到了栈或堆栈,它的结构特点是什么呢?先进后出,它的特点有什么用呢?我们在哪里可以使用到栈结构,栈结构那么简单,使用这么久了为什么不用其它结构替代? 一.程序在内存中的 ...

  6. JAVA 使用IText7 + Freemarker 动态数据生成PDF实现案例

    技术方案:IText7 + Freemarker 技术文档 Itext 官网:https://itextpdf.com/ itext API文档:https://api.itextpdf.com/iT ...

  7. python里class(类)中object是什么意思

    python的class(类)中的object是什么意思? 写object和不写object有什么区别? 示例代码 class Test: name = "libai" class ...

  8. 【RS】ENVI5.6.3 图像融合

            图像融合是将低空间分辨率的多光谱图像或高光谱数据与高空间分辨率的单波段图像重采样生成一幅高分辨率多光谱图像的遥感图像处理技术,使得处理后的图像既有较高的空间分辨率,又具有多光谱特征.一 ...

  9. @SuppressWarnings注解的使用

    Java编译器在编译代码时,会产生一些安全警告信息.如果被@SuppressWarnings注解标记的元素,就可以告诉编译器抑制指定的警告. 先看看@SuppressWarnings注解在Java S ...

  10. 1. CS和BS的优缺点

    1. CS CS : 客户端服务器架构模式 优点 : 充分利用客户端机械的资源 , 减轻服务器的符合 缺点 : 需要安装 : 升级维护成本较高 ‍ 2. BS ‍ 优点 : 客户端不需要安装 : 维护 ...