Spring bean的生命周期

          ApplicationContext 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,就是为了执行初始化之后的操作 ,但是对spring产生了依赖
* 后续使用反射机制 init-method 来消除对spring的依赖
* 04.实现了DisposableBean,就是为了执行bean销毁之后的操作 ,但是对spring产生了依赖
* 后续使用反射机制 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(二):bean的生命周期

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

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

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

  10. spring框架中Bean的生命周期

    一.Bean 的完整生命周期 在传统的Java应用中,bean的生命周期很简单,使用Java关键字 new 进行Bean 的实例化,然后该Bean 就能够使用了.一旦bean不再被使用,则由Java自 ...

随机推荐

  1. 基于Python原生asyncio模块对DNS正向和反向的解析

    一.正向解析:域名解析IP地址 import asyncio import socket domains = [ ('www.baidu.com', 'https'), ('cn.bing.com', ...

  2. 22. ClustrixDB 杀掉恶意会话

    ClustrixDB提供了几种机制来识别消耗大量系统资源的查询.这样的查询通常是应用程序索引不良或错误的结果. ClustrixDB支持以下语法来杀死查询: KILL [QUERY | CONNECT ...

  3. jquery text选择器 语法

    jquery text选择器 语法 作用::text 选择器选取类型为 text 的 <input> 元素.大理石平台检定规程 语法:$(":text") jquery ...

  4. webpack3升级webpack4

    cnpm i webpck@4 webpack-cli -D cnpm i webpack-cli -D cnpm update npm WARN deprecated extract-text-we ...

  5. MySQL5.7忘记root密码-手动修改密码教程

    https://blog.csdn.net/greywolf0824/article/details/80215199

  6. jquery数组的合并 对象的合并

    直接转自:https://www.cnblogs.com/ooo0/p/7737678.html 1.数组的合并 1.1 concat方法 var a=[1,2,3],b=[4,5,6]; var c ...

  7. leetcode-easy-string-28 Implement strStr()

    mycode   77.15% class Solution(object): def strStr(self, haystack, needle): """ :type ...

  8. vue问题四:富文本编辑器上传图片

    vue使用富文本编辑器上传图片: 我是用的是wangEditor 富文本编辑器 demo:http://www.wangeditor.com/ 1).安装依赖:npm install wangedit ...

  9. 代码实现:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。 已抽签决定比赛名单。有人向队员打听比赛的名单。 a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单

    /*两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人. 已抽签决定比赛名单.有人向队员打听比赛的名单. a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单.*/ ...

  10. Select 选择器

    Select 选择器 当选项过多时,使用下拉菜单展示并选择内容. 基础用法 适用广泛的基础单选 v-model的值为当前被选中的el-option的 value 属性值 <template> ...