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. linux运维、架构之路-K8s应用

    一.Deployment         k8s通过各种Controller管理Pod的生命周期,为了满足不同的业务场景,k8s提供了Deployment.ReplicaSet.DaemonSet.S ...

  2. Springboot 使用JPA

    Springboot 使用jpa maven依赖 <dependency> <groupId>org.springframework.boot</groupId> ...

  3. c++匿名函数精简写法

    main.cpp: #include<stdio.h> #include<functional> #include<unistd.h> std::function& ...

  4. java 正则表达式:有丶东西

    非常详细 原文地址:https://blog.csdn.net/jeffleo/article/details/52194977

  5. Leetcode: 二分搜索法

    package com.LeetCode; /** * 算法:二分搜索法查找一个值,并返回索引值 * https://leetcode.com/problems/search-insert-posit ...

  6. Jenkins+Harbor+Docker发布

    使用Jenkins发布Docke 需要准备的,docker,jenkins,Harbor docker安装 安装依赖: # yum install -y yum-utils device-mapper ...

  7. redis详解及应用(雪崩、击穿、穿透)

    一. redis的简介与安装 引用:https://www.cnblogs.com/ysocean/tag/Redis%E8%AF%A6%E8%A7%A3/ 二. redis的配置文件介绍 引用:ht ...

  8. 【3】火狐中: radio被点击以后,重刷页面,不会选择默认的radio

    1.问题:火狐中radio (单选框)点击以后,重新刷新页面,不会选择默认的radio 解决:form表单中添加:autocomplete="off" autocomplete 属 ...

  9. java下载文件时文件名出现乱码的解决办法

    转: java下载文件时文件名出现乱码的解决办法 2018年01月12日 15:43:32 橙子橙 阅读数:6249   java下载文件时文件名出现乱码的解决办法: String userAgent ...

  10. Web(八) commons-fileupload上传下载

    在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6479405.html>,在此仅供学习参考之用. 一.上传 ...