ssm框架之spring: bean的生命周期
创始人
2024-05-30 04:56:55
0

bean的生命周期

其实前面聊servlet的时候也聊过生命周期,那么通过spring创建bean自然也有其生命周期,先说生命周期,然后再具体演示:

  • 通过构造器创建bean实例,无论是有参无参都会先调用,毕竟不创建对象,后面的也就没有必须操作了。
  • bean的初始化前通过后置处理器添加方法。
  • 调用bean的初始化方法,这个配置初始化的方法。说白就是对属性进行赋值。
  • bean的初始化后通过后置处理器添加方法。
  • 为bean的属性设置时和对其它bean的引用,简单的说就是通过set方法。
  • 可以得到bean的对象。
  • 当容器关闭的时候,调用bean的销毁方法,但是这个需要配置销毁。

如果不明白实例化和初始化的区别,可以看另一篇文章:传送阵

首先创建一个java类Student:

package com.xzd.bean;public class Student   {int age;String name;public Student( ) {System.out.println("实例化");}public Student(int age, String name) {this.age = age;this.name = name;}public void setAge(int age) {System.out.println("给学生设置年龄");this.age = age;}public void setName(String name) {System.out.println("给学生设置名字");this.name = name;}public void initMethon(){System.out.println("init--初始化方法");}public void destoryMethon(){System.out.println("destory--销毁方法");}@Overridepublic String toString() {return "Student{" +"age=" + age +", name='" + name + '\'' +'}';}
}

配置的xml文件:



在bean标签有两个属性一定要注意:

  • init-method: 这个是配置spring中调用的初始化方法;
  • destroy-method: 这个是配置spring调用的销毁方法,而这个销户的时候调用的方法。

现在开始调用方法:

public class testSpring {public static void main(String[] args) {// ApplicationContext applicationContext=   new ClassPathXmlApplicationContext("spring_test.xml");
//        因为ApplicationContext 没有关闭关闭方法,所以无法调用destroy-method,所以需要用 ConfigurableApplicationContextConfigurableApplicationContext configurableApplicationContext=new ClassPathXmlApplicationContext("spring_test.xml");Student student = configurableApplicationContext.getBean("student", Student.class);student.setAge(10);System.out.println(student);configurableApplicationContext.close();}
}

在这里插入图片描述

作用域对bean生命周期的影响

将上面的xml配置修改为一下两种:

  • 单例模式:

    
    
    
  • 多例模式:

    
    
    

这样依次调用:

public class testSpring {public static void main(String[] args) {// ApplicationContext applicationContext=   new ClassPathXmlApplicationContext("spring_test.xml");
//        因为ApplicationContext 没有关闭关闭方法,所以无法调用destroy-method,所以需要用 ConfigurableApplicationContextConfigurableApplicationContext configurableApplicationContext=new ClassPathXmlApplicationContext("spring_test.xml");Student student = configurableApplicationContext.getBean("student", Student.class);student.setAge(10);System.out.println(student);configurableApplicationContext.close();}
}

在这里插入图片描述

都是这个结果,好像没有什么不同。

然后只是加载配置文件代码如下:

public class testSpring {public static void main(String[] args) {// ApplicationContext applicationContext=   new ClassPathXmlApplicationContext("spring_test.xml");
//        因为ApplicationContext 没有关闭关闭方法,所以无法调用destroy-method,所以需要用 ConfigurableApplicationContextConfigurableApplicationContext configurableApplicationContext=new ClassPathXmlApplicationContext("spring_test.xml");//Student student = configurableApplicationContext.getBean("student", Student.class);// student.setAge(10);//System.out.println(student);// configurableApplicationContext.close();}
}

**单例模式 **下结果是:

在这里插入图片描述

**多例模式 **下结果是:

在这里插入图片描述

这样可以看出单例模式下加载配置文件的时候bean会直接创建好的,像是饿汉模式,毕竟创建对象后不会变,所以提前创建好,以供使用。

多例模式下bean加载配置文件的时候,有点像是懒汉模式了,就是什么时候使用,什么时候创建bean,毕竟每次创建都不同,所以没有必要提前创造好。

bean的后置处理器: 初始化前后添加方法

前面写spring中的的生命周期的时候,提到了一个初始化前后添加方法,而前面的例子中没有体现。因为这个用到了一个bean的后置处理器。

  • 需要实现BeanPostProcessor接口。
  • 需要通过配置配置给Spring容器中,这个需要注意一点,bean的后置处理器不是单独针对某一个bean生效,而是针对所有的spring容器中的bean生效。

现在开始演示:

这个需要单独创建一个实现BeanPostProcessor接口的接口类:

public class Myprocess implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("初始化前beanName="+ beanName+"  bean="+bean);return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("初始化后beanName="+ beanName+"  bean="+bean);return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);}}

然后在配置文件中配置后置处理器:



然后调用:

public class testSpring {public static void main(String[] args) {// ApplicationContext applicationContext=   new ClassPathXmlApplicationContext("spring_test.xml");
//        因为ApplicationContext 没有关闭关闭方法,所以无法调用destroy-method,所以需要用 ConfigurableApplicationContextConfigurableApplicationContext configurableApplicationContext=new ClassPathXmlApplicationContext("spring_test.xml");Student student = configurableApplicationContext.getBean("student", Student.class);student.setAge(10);System.out.println(student);configurableApplicationContext.close();}
}

在这里插入图片描述

然后作用域对其影响和带不带后置处理器效果几乎意义

  • 如果是单例

在这里插入图片描述

  • 如果是多例:

在这里插入图片描述

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...