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. 如何查看 SQL Server 执行的历史 SQL 语句记录?

    SELECT st.text as sql_statement, qs.creation_time as plan_last_compiled, qs.last_execution_time as p ...

  2. sublime text怎么格式化PHP代码

    手动安装: 可能由于各种原因,无法使用代码安装,那可以通过以下步骤手动安装Package Control: 1.点击Preferences > Browse Packages菜单 2.进入打开的 ...

  3. sh_05_列表遍历

    sh_05_列表遍历 name_list = ["张三", "李四", "王五", "王小二"] # 使用迭代遍历列表 ...

  4. 6.集成算法boosting----AdaBoost算法

    1.提升算法 提升算法实为将一系列单一算法(如决策树,SVM等)单一算法组合在一起使得模型的准确率更高.这里先介绍两种Bagging(代表算法随机森林),Boosting(代表算法AdaBoost-即 ...

  5. KMP 串的模式匹配 (25 分)

    给定两个由英文字母组成的字符串 String 和 Pattern,要求找到 Pattern 在 String 中第一次出现的位置,并将此位置后的 String 的子串输出.如果找不到,则输出“Not ...

  6. unittest详解(四) 批量执行用例(discover)

    前面我们说了,对于不同文件用例,我们可以通过addTest()把用例加载到一个测试套件(TestSuite)来统一执行,对于少量的文件这样做没问题,但是如果有几十上百个用例文件,这样做就太浪费时间了. ...

  7. CodeForces451E Devu and Flowers

    题目链接 问题分析 没有想到母函数的做法-- 其实直接看题思路挺简单的.发现如果每种花都有无限多的话,问题变得十分简单,答案就是\(s+n-1\choose n - 1\).然后发现\(n\)只有\( ...

  8. Socket编程-基础使用

    最后更新:2019-10-25 一 基本概念 socket, 又称为"套接字"或者"插座". 是操作系统提供的一种进程间通信机制.目前大多用于不同网络设备之间的 ...

  9. vue中的js绑定样式

    添加class 对象形式添加   activated为true时p标签的class为activated false时为空 <div id="app"> <p :c ...

  10. linux 中的 "2>&1"含义

    文章摘自:http://os.chinaunix.net/a2009/0903/996/000000996941.shtml 脚本是: nohup /mnt/Nand3/H2000G >/dev ...