Bean的生命周期

article/2025/8/24 3:20:23

目录

一,bean的初始化

(1)Spring的IOC和AOP:

(2)Spring Bean的生命周期:

二,单例模式与多例模式的区别

区别

 《代码演示》


前言 

回顾:Servlet的生命

初始化:init-----》Tomcat启动,servlet对象就创建/初始化了

服务:servlet----》浏览器发送请求,对应的servlet进行处理调用

销毁:destroy----》Tomcat停止

一,bean的初始化

如图

(1)Spring的IOC和AOP:

                //初始化Spring上下文容器(IOC)

                             ioc是一个容器,Spring是管理所有的Javabean对象;

                                这些Javabean对象什么时候生,什么时间提供服务,什么时候销毁

初始化Spring容器的时候,是通过

ApplicationContext ac =  new ClassXmlPathApplicationContext("spring.xml");对象始化的对应的就是图中xml

(2)Spring Bean的生命周期:

1)通过XML、Java annotation(注解)以及Java Configuration(配置类)

等方式加载Spring Bean

2)BeanDefinitionReader解析Bean的定义(图中含有)。在Spring容器启动过程中,

会将Bean解析成Spring内部的BeanDefinition结构;

理解为:将spring.xml中的<bean>标签转换成BeanDefinition结构

有点类似于XML解析

3)BeanDefinition:包含了很多属性和方法。例如:id、class(类名)、

scope、ref(依赖的bean)等等。其实就是将bean(例如<bean>)的定义信息

存储到这个对应BeanDefinition相应的属性中

例如:

<bean id="" class="" scope=""> -----> BeanDefinition(id/class/scope)

4)BeanFactoryPostProcessor:是Spring容器功能的扩展接口。

注意:

1)BeanFactoryPostProcessor在spring容器加载完BeanDefinition之后,

在bean实例化之前执行的

2)对bean元数据(BeanDefinition)进行加工处理,也就是BeanDefinition

属性填充、修改等操作

5)BeanFactory:bean工厂。它按照我们的要求生产我们需要的各种各样的bean。

例如:

BeanFactory -> List<BeanDefinition>

BeanDefinition(id/class/scope/init-method)

<bean class="com.zking.spring02.biz.BookBizImpl"/>

foreach(BeanDefinition bean : List<BeanDefinition>){

   //根据class属性反射机制实例化对象

   //反射赋值设置属性

}

6)Aware感知接口:在实际开发中,经常需要用到Spring容器本身的功能资源

例如:BeanNameAware、ApplicationContextAware等等

BeanDefinition 实现了 BeanNameAware、ApplicationContextAware

7)BeanPostProcessor:后置处理器。在Bean对象实例化和引入注入完毕后,

在显示调用初始化方法的前后添加自定义的逻辑。(类似于AOP的绕环通知)

前提条件如果检测到Bean对象实现了BeanPostProcessor后置处理器才会执行

Before和After方法

BeanPostProcessor

1)Before

2)调用初始化Bean(InitializingBean和init-method,Bean的初始化才算完成)

3)After

完成了Bean的创建工作

8)destory:销毁

简单总结

        1.通过三种方式(配置文件,注解,配置类)将bean便签转成BeanDefinition对象

·       2.通过BeanFactoryPostProcessor可以在初始化之前修啊属性值

        3.BeanFactory进行bean实例化,说白了就是生产Javabean

        4.Aware感知接口,能够在拿到Spring上下文中内部的资源对象

        5.BeanPostProcessor后置处理器,相当于环绕通知

《代码测试》

在正式初始化之前会调用init方法,而我调用的this.init();方法会有专门的人员来写,就是BeanFactoryPostProcessor

《提取代码》

public Person() {this.init();this.name="hhh";this.sex="nan";this.age=66;}private void init() {// TODO Auto-generated method stub}

《整体代码》 

package com.zking.beanLife;public class Demo1 {public static void main(String[] args) {Person p = new Person();p.setSex("nan");System.out.println(p.getSex());}
}class Person{private String name;private String sex;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Person() {this.init();this.name="hhh";this.sex="nan";this.age=66;}private void init() {// TODO Auto-generated method stub}@Overridepublic String toString() {return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]";}}

二,单例模式与多例模式的区别

区别

单例时容器销毁instanceFactory对象也销毁;

        单例模式下Javabean的生命周期:容器生对象生,容器死对象死

 ​​​​​​​       优点:内存使用少

        弊端:存在变量污染

        

补充:当配置文件中没有scope=“”时默认就是单例,当scope=“prototype”中填写的是

prototype就是原型的模式(多例模式)但是Spring容器中默认的是单例singleton

多例时容器销毁对象不一定销毁;

        多例模式下Javabean的生命周期:使用时对象生,死亡跟着JVM垃圾回收站机制走

        bean的初始化时间点,除了与bean管理模式(单例/多例)有关,还跟beanfactor的子类有关

        优点:变量相对独立

​​​​​​​        弊端:内存占比较多

 《代码演示》

package com.zking.beanLife;import java.util.List;/*** 验证单例多例模式的区别* @author dxy**/
public class ParamAction {private int age;private String name;private List<String> hobby;//当初始化值是1private int num = 1;// private UserBiz userBiz = new UserBizImpl1();public ParamAction() {super();}public ParamAction(int age, String name, List<String> hobby) {super();this.age = age;this.name = name;this.hobby = hobby;}public void execute() {// userBiz.upload();// userBiz = new UserBizImpl2();System.out.println("this.num=" + this.num++);System.out.println(this.name);System.out.println(this.age);System.out.println(this.hobby);}
}

 num的值不一样,是由一个类创建了两个对象,但是两个对象是同一个对象,这就是单例

package com.zking.beanLife;/*** 为了验证BeanPostrocessor 初始化Javabean* @author dxy**/
public class InstanceFactory {public void init() {System.out.println("初始化方法");}public void destroy() {System.out.println("销毁方法");}public void service() {System.out.println("业务方法");}
}
package com.zking.beanLife;import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;/** spring	bean的生命週期* spring	bean的單例多例*/
public class Demo2 {// 体现单例与多例的区别@Testpublic void test1() {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
//		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
//		ParamAction p1 = (ParamAction) applicationContext.getBean("paramAction");
//		ParamAction p2 = (ParamAction) applicationContext.getBean("paramAction");// System.out.println(p1==p2);InstanceFactory p1 = (InstanceFactory) applicationContext.getBean("instanceFactory");InstanceFactory p2 = (InstanceFactory) applicationContext.getBean("instanceFactory");
//		p1.execute();
//		p2.execute();//		单例时,容器销毁instanceFactory对象也销毁;多例时,容器销毁对象不一定销毁;applicationContext.close();}// 体现单例与多例的初始化的时间点 instanceFactory@Testpublic void test2() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");}// BeanFactory会初始化bean对象,但会根据不同的实现子类采取不同的初始化方式// 默认情况下bean的初始化,单例模式立马会执行,但是此时XmlBeanFactory作为子类,单例模式下容器创建,bean依赖没有初始化,只有要获取使用bean对象才进行初始化@Testpublic void test3() {// ClassPathXmlApplicationContext applicationContext = new// ClassPathXmlApplicationContext("/spring-context.xml");Resource resource = new ClassPathResource("/spring-context.xml");BeanFactory beanFactory = new XmlBeanFactory(resource);
//		InstanceFactory i1 = (InstanceFactory) beanFactory.getBean("instanceFactory");}}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!--辅助理理解的配置  --><bean id="paramAction" class="com.zking.beanLife.ParamAction" scope="poprototype"><constructor-arg name="name" value="三丰"></constructor-arg><constructor-arg name="age" value="21"></constructor-arg><constructor-arg name="hobby"><list><value>抽烟</value><value>烫头</value><value>大保健</value></list></constructor-arg></bean><bean id="instanceFactory" class="com.zking.beanLife.InstanceFactory"scope="prototype" init-method="init" destroy-method="destroy"></bean>
</beans>

效果图

 当在配置文件中加上scope=“”里面的值换成prototype多例模式,可以看到效果图,num值就变得不一样了


http://chatgpt.dhexx.cn/article/1RK33hY3.shtml

相关文章

bean的生命周期(最全最细讲解)

一、bean生命周期&#xff1a; 其定义为&#xff1a;从对象的创建到销毁的过程。而Spring中的一个Bean从开始到结束经历很多过程&#xff0c;但总体可以分为六个阶段Bean定义、实例化、属性赋值、初始化、生存期、销毁。 二、案例代码演示 1.首先我们来创建一个包&#xff0…

去字节面试,直接让人出门左拐:Bean 生命周期都不知道!

Spring Bean 的生命周期&#xff0c;面试时非常容易问&#xff0c;这不&#xff0c;前段时间就有个粉丝去字节面试&#xff0c;因为不会回答这个问题&#xff0c;一面都没有过。 如果只讲基础知识&#xff0c;感觉和网上大多数文章没有区别&#xff0c;但是我又想写得稍微深入…

Spring Bean生命周期,好像人的一生。。

大家好&#xff0c;我是老三&#xff0c;上节我们手撸了一个简单的IOC容器五分钟&#xff0c;手撸一个Spring容器&#xff01;&#xff0c;这节我们来看一看Spring中Bean的生命周期&#xff0c;我发现&#xff0c;和人的一生真的很像。 简单说说IoC和Bean IoC&#xff0c;控制…

Spring中bean的生命周期

Spring中的bean的生命周期主要包含四个阶段&#xff1a;实例化Bean --&#xff1e; Bean属性填充 --&#xff1e; 初始化Bean --&#xff1e;销毁Bean 首先是实例化Bean&#xff0c;当客户向容器请求一个尚未初始化的bean时&#xff0c;或初始化bean的时候需要注入另一个尚末初…

一文读懂 Spring Bean 的生命周期

欢迎大家关注我的微信公众号【老周聊架构】&#xff0c;Java后端主流技术栈的原理、源码分析、架构以及各种互联网高并发、高性能、高可用的解决方案。 一、前言 今天我们来说一说 Spring Bean 的生命周期&#xff0c;小伙伴们应该在面试中经常遇到&#xff0c;这是正常现象。…

ubuntu 换源深层次解析

换源也是一个容易出错的问题&#xff0c;本文以树莓派为例展开&#xff0c;x86也是一样的操作。 那么假设成立的话&#xff0c;就要记住我们是在树莓派&#xff08;arm&#xff09;上安装的ubuntu&#xff0c;不是X86&#xff0c;不是amd。 安装好系统后&#xff0c;我们第一…

[Linux]Ubuntu 换源 20.04 阿里源

注意&#xff0c;这篇文章其实不是简单的教你怎么换源&#xff0c;而是示例一种方法来换20.04的阿里源&#xff0c;其他源和版本大同小异。 笔者在写这篇文章的时候&#xff0c;20.04 还没有release出来正式版&#xff0c;但是已经可以在仓库里看到有源存在了&#xff0c;故写下…

Ubuntu换源详解,教你如何换源,并且解决常见的大坑

Ubuntu换源详解&#xff0c;教你如何换源&#xff0c;并且解决常见的大坑 记一次极不愉快的一次经历 首先注意&#xff0c;换源必须选择合适的版本&#xff0c;不可以在网上找一个下载源就直接去换 出现错误1&#xff1a; 由于没有公钥&#xff0c;无法验证下列签名 :NO_PUBK…

ubuntu 换源

网上应该可以找到很多关于ubuntu源的设置方法&#xff0c;但是如果不搞清楚就随便设置的话&#xff0c;不仅不能起到应有的效果&#xff0c;还会由于一些问题导致apt不可用。 最正确的更换源的方法应该如系统提示的&#xff1a; ## a.) add apt_preserve_sources_list: true …

20.04Ubuntu换源:提升软件下载速度和更新效率

在使用Ubuntu操作系统时&#xff0c;一个常见的优化措施是更改软件源&#xff0c;以提高软件下载速度和更新效率。软件源是指存储软件包的服务器&#xff0c;通过更换软件源&#xff0c;你可以选择更靠近你所在地区的服务器&#xff0c;从而加快软件下载速度&#xff0c;并减少…

Ubuntu快速更换源

Ubuntu更换源&#xff0c;零基础操作 提示&#xff1a;跟着一步步来&#xff0c;很快完成操作 为什么要换源&#xff1f;安装的linux系统默认的源是国外的&#xff0c;当用命令行安装软件&#xff08;比如安装gcc&#xff09;时下载速度非常慢&#xff0c;这里将源换成国内阿里…

Ubuntu系统换源

简单介绍一下源&#xff0c;源就是一个大仓库&#xff08;类似应用商店&#xff09;&#xff0c;系统下载软件需要从这个仓库下载&#xff0c;因为Ubuntu默认源是国外的&#xff0c;所以在下载东西的时候会出现下载速度很慢的情况&#xff0c;所以我们需要更换成国内的源&#…

ubuntu linux 换源,给Ubuntu换源

新手在使用Ubuntu的时候可能在升级时感觉很慢&#xff0c;如果这样他就需要换一个适合自己的源了。 下面我就简单的说一下怎样换源。 在终端里输入 sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup (表示备份列表) 再输入 sudo gedit /etc/apt/sources.list 你就能…

ubuntu换源

ubuntu源 ubuntu换源方法1. 图形界面配置&#xff08;新手推荐&#xff09;2. 手动更改配置文件2.1 命令行替换2.2 手动替换 ubuntu的镜像源阿里源清华源中科大源 ubuntu换源方法 1. 图形界面配置&#xff08;新手推荐&#xff09; 依次打开&#xff1a;系统设置&#xff0c;…

Ubuntu换源的两种方法

Ubuntu系统自带的是国外的源,咱们国内用户使用的时候下载文件特别的慢,所以我们需要更换国内镜像源,这里我列举两个换源的方法,如果有新的方法可以在评论区补充。 首先第一步 我们需要更改root密码 sudo passwd root命令行: 首先备份源 也可以不备份 sudo cp /etc/apt/sou…

ubuntu更换源(清华源)并更新系统

首先更新自己需要的源 &#xff08;1&#xff09;打开清华园官网https://mirrors.tuna.tsinghua.edu.cn/&#xff1a; &#xff08;2&#xff09;搜索Ubuntu&#xff1a; &#xff08;3&#xff09;点击旁边的问号&#xff1a; &#xff08;4&#xff09;选择自己ubuntu的版…

Ubuntu更换国内源(apt更换源)

网上的教程大部分都是文本命令行的方式更换国内源的&#xff0c;其实Ubuntu18.04也提供了图形界面的方式&#xff0c;这里主要讲图形界面的方式&#xff0c;毕竟点点鼠标就能完成的事儿谁愿意去输命令啊&#xff0c;而且还容易出错&#xff0c;当然这里也附上命令行的方式。 可…

ubuntu换镜像源(ubuntu换源)

换源 Ubuntu中 大部分 的软件 安装/更新 都是利用 apt命令&#xff0c;从ubuntu的服务器 直接安装的 Ubuntu官方的服务器在国外&#xff0c;为了提高软件 安装/更新速度&#xff0c;ubuntu提供了 选择最佳服务器 的功能&#xff0c;可以帮助我们方便的找到一个速度最快的 镜像…

使用多线程往LIST添加数据 线程安全list

我们在日常写代码的过程中&#xff0c;经常会使用多线程提高效率&#xff0c;我们在使用多线程过程中难免会出现往List集合修改数据。 下面我们来尝试一下往ArrayList 添加数据&#xff1a; public static void main(String[] args) {List<Integer> list new ArrayList…

集合线程安全

集合线程安全 常用的集合类型如ArrayList&#xff0c;HashMap&#xff0c;HashSet等&#xff0c;在并发环境下修改操作都是线程不安全的&#xff0c;会抛出java.util.ConcurrentModificationException异常&#xff0c;这节主要记录如何在并发环境下安全地修改集合数据。 List…