Spring(三)--Spring bean的生命周期

时间:2022-09-23 12:41:02

Spring bean的生命周期

          ApplicationContext Bean生命周期流程

Spring(三)--Spring bean的生命周期

1.需要的实体类

ackage com.xdf.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*; /**
* 学生的实体类
*
* Aware本意就是察觉,感觉
* 01.实现了BeanNameAware,就是让student类感觉到自己在容器中的id或者是name
* 02.实现了BeanFactoryAware,就是让student类感觉到自己在容器中所属的bean工厂
* 03.实现了InitializingBean,就是为了执行初始化之后的操作 ,但是对sprin*生了依赖
* 后续使用反射机制 init-method 来消除对spring的依赖
* 04.实现了DisposableBean,就是为了执行bean销毁之后的操作 ,但是对sprin*生了依赖
* 后续使用反射机制 destroy-method 来消除对spring的依赖
*/
public class Student implements BeanNameAware,BeanFactoryAware,InitializingBean,DisposableBean{
private int age; //年龄
private String stuName; //姓名 private String beanName; //bean在容器中的id或者name
private BeanFactory beanFactory; //bean所在的工厂 public Student() {
System.out.println("===Student类中的无参构造===");
} //BeanNameAware接口中的setBeanName()
public void setBeanName(String beanName) {
System.out.println("===执行了BeanNameAware接口中的setBeanName()===");
this.beanName=beanName;
} //BeanFactoryAware中的setBeanFactory()
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("===执行了BeanFactoryAware中的setBeanFactory()===");
this.beanFactory=beanFactory;
} public void initMethod(){
System.out.println("===Student类中的initMethod()===");
} public void afterPropertiesSet() throws Exception {
System.out.println("===InitializingBean中的afterPropertiesSet()===");
} public void myDestroy(){
System.out.println("===Student类中的myDestroy()===");
} public void destroy() throws Exception {
System.out.println("===DisposableBean中的destroy()===");
} public int getAge() {
return age;
} public void setAge(int age) {
System.out.println("===Student类中给属性赋值 setAge()===");
this.age = age;
} public String getStuName() {
return stuName;
} public void setStuName(String stuName) {
System.out.println("===Student类中给属性赋值 setStuName()===");
this.stuName = stuName;
} public String getBeanName() {
return beanName;
} public BeanFactory getBeanFactory() {
return beanFactory;
} @Override
public String toString() {
return "Student{" +
"age=" + age +
", stuName='" + stuName + '\'' +
'}';
}

2.需要的InstantiationAwareBeanPostProcessorAdapter

package com.xdf.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; import java.beans.PropertyDescriptor; /**
* bean实例化之前 和之后
*/
public class MyInitAwareBeanpostAdpater extends InstantiationAwareBeanPostProcessorAdapter{ public MyInitAwareBeanpostAdpater(){
System.out.println("*****MyInitAwareBeanpostAdpater的无参构造*****");
} //在实例化bean之前调用
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("*****执行了MyInitAwareBeanpostAdpater的 postProcessBeforeInstantiation *****");
return null; //底层返回的就是null
} //在实例化bean之后调用
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("*****执行了MyInitAwareBeanpostAdpater的 postProcessAfterInitialization *****");
return bean;
}
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
System.out.println("*****执行了MyInitAwareBeanpostAdpater的 postProcessPropertyValues *****");
return pvs;
} }

3.需要的BeanPostProcessor

package com.xdf.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; /**
* Processor 本意是加工 处理的意思!
*
* 实现了BeanPostProcessor
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
public MyBeanPostProcessor(){
System.out.println("===MyBeanPostProcessor的无参构造方法 ===");
} public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("===执行了BeanPostProcessor中的 postProcess ==Before==Initialization ===");
return bean;
} public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("===执行了BeanPostProcessor中的 postProcess ==After==Initialization ===");
return bean;
}
}

4.需要的BeanFactoryPostProcessor

package com.xdf.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; /**
* Processor 本意是加工 处理的意思!
*
* 实现了BeanFactoryPostProcessor 工厂的后处理器
*/
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public MyBeanFactoryPostProcessor(){
System.out.println("===MyBeanFactoryPostProcessor的无参构造方法 ===");
} public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("===MyBeanFactoryPostProcessor的postProcessBeanFactory ===");
BeanDefinition beanDefinition = beanFactory.getBeanDefinition("student");
beanDefinition.getPropertyValues().addPropertyValue("stuName","小白");
}
}

5.需要的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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置MyBeanPostprocessor 容器级别的 当前xml文件中所有的bean都会执行MyBeanPostProcessor中的方法-->
<bean class="com.xdf.bean.MyBeanPostProcessor"/>
<!--配置MyBeanFactoryPostprocessor 容器级别的 同上-->
<bean class="com.xdf.bean.MyBeanFactoryPostProcessor"/>
<!--配置MyInitAwareBeanpostAdpater 容器级别的 同上-->
<bean class="com.xdf.bean.MyInitAwareBeanpostAdpater"/> <!-- 配置Student 的实体对象-->
<bean id="student" class="com.xdf.bean.Student" init-method="initMethod" destroy-method="myDestroy">
<property name="age" value="20"/>
<property name="stuName" value="小黑"/>
</bean>
</beans>

6.需要的测试代码

/**
* 测试bean生命周期
*/
public class LifeCycle { public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Student student= context.getBean("student", Student.class);
System.out.println(student);
((ClassPathXmlApplicationContext)context).close(); //关闭容器
}
}

  

    未完待续!!!

Spring(三)--Spring bean的生命周期的更多相关文章

  1. (转)Spring管理的Bean的生命周期

    http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...

  2. Spring 容器中 Bean 的生命周期

    Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...

  3. (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  4. Spring 学习笔记---Bean的生命周期

    生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...

  5. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

    Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...

  6. Spring实战(二)Spring容器和bean的生命周期

    引入问题: 在XML配置文件中配置bean后,这些文件又是如何被加载的?它们被加载到哪里去了? Spring容器——框架核心 1.什么是Spring容器?它的功能是什么? 在基于Spring的应用中, ...

  7. Spring基础14——Bean的生命周期

    1.IOC容器中的Bean的生命周期方法 SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务.SpringIOC容器对Bean的生命周期进行管理 ...

  8. spring&lpar;二&rpar;:bean的生命周期

    bean的生命周期指的是bean的创建——>初始化——>销毁的过程,该过程是由spring容器进行管理的 我们可以自定义bean初始化和销毁的方法:容器在bean进行到当前生命周期时,调用 ...

  9. IoC基础篇(一)--- Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  10. spring中的bean的生命周期

    bean的生命周期:bean的创建 —— 初始化 ——销毁的过程 容器管理bean的生命周期,我们可以自定义初始化和销毁方法,容器在bean进行到当前生命周期就会调用我们的方法 在xml配置文件中是在 ...

随机推荐

  1. Switch&NAT 测试

    测试环境: PC1:Windows10 iperf3 PC2:Ubuntu iperf3 都装有千兆网卡,直连的iperf速度是935Mbps. 因为TXRX两个方向的数据是差不多的,下面的测试数据只 ...

  2. canvas动画

    1.动画主要是requestAnimationFrame方法,现在我们来一步步实现一个在画布内滚动的实例. html代码: <canvas id="canvas" width ...

  3. Mac Pro 使用 ll、la、l等ls的别名命令

    在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...

  4. 在线调试和演示的前端开发工具------http&colon;&sol;&sol;jsfiddle&period;net&sol;

    在线调试和演示的前端开发工具------http://jsfiddle.net/

  5. Python之list添加新元素、删除元素、替换元素

    Python之list添加新元素 现在,班里有3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 今天,班里转来一名新同学 Paul,如何把新同学添加到现有 ...

  6. c语言 列出系统进程

    #include <stdio.h> #include "stdafx.h" #include <Windows.h> #include <strin ...

  7. 高级UIKit-06&lpar;UIImagePickerController&rpar;

    [day07-1-getSystemImage]:获取系统相册 UIImagePickerController图片采集控制器 picker采集者,采摘者 该方法继承自:UINavigationCont ...

  8. MySQL自动化(全量&plus;增量)备份脚本

    文章转自:http://www.it-hack.cn/forum.php?mod=viewthread&tid=220&extra=page%3D1 一.MySQL的日常备份方案: 全 ...

  9. FusionCharts封装-单系列图

    ColumnChart.java: /** * @Title:ColumnChart.java * @Package:com.fusionchart.model * @Description:柱形图 ...

  10. Basic berkeley socket functions

    gethostbyname() DNS を通して.Domain の Information を GET する.例えば IP Address なんだ. げん型: #include <netdb.h ...