Spring(三)--Spring bean的生命周期
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的生命周期的更多相关文章
- (转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...
- Spring 容器中 Bean 的生命周期
Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...
- (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期
日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...
- Spring 学习笔记---Bean的生命周期
生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...
- Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)
Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...
- Spring实战(二)Spring容器和bean的生命周期
引入问题: 在XML配置文件中配置bean后,这些文件又是如何被加载的?它们被加载到哪里去了? Spring容器——框架核心 1.什么是Spring容器?它的功能是什么? 在基于Spring的应用中, ...
- Spring基础14——Bean的生命周期
1.IOC容器中的Bean的生命周期方法 SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务.SpringIOC容器对Bean的生命周期进行管理 ...
- spring(二):bean的生命周期
bean的生命周期指的是bean的创建——>初始化——>销毁的过程,该过程是由spring容器进行管理的 我们可以自定义bean初始化和销毁的方法:容器在bean进行到当前生命周期时,调用 ...
- IoC基础篇(一)--- Spring容器中Bean的生命周期
日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...
- spring框架中Bean的生命周期
一.Bean 的完整生命周期 在传统的Java应用中,bean的生命周期很简单,使用Java关键字 new 进行Bean 的实例化,然后该Bean 就能够使用了.一旦bean不再被使用,则由Java自 ...
随机推荐
- CSS 按钮
总结有关按钮的各种样式 ㈠基本按钮样式 看一下没有进行css样式设计时按钮的样子与进行样式设计的按钮样子 <!DOCTYPE html> <html> <head> ...
- Java多线程和并发(一),进程与线程的区别
目录 1.进程和线程的由来 2.进程和线程的定义 3.进程和线程的区别 一.进程和线程的区别 1.进程和线程的由来 2.进程和线程的定义 进程是资源分配的最小单位,线程是CPU调度的最小单位 3.进程 ...
- Selenium 环境安装
前言: 本人在学习Selenium时,用的版本是Python3.6+Selenium3,后续写的所有学习资料都是基于这套环境.在安装Selenium3前,请确保本机已安装好了Python3,如未安装可 ...
- 用node.js搭建一个静态资源站 html,js,css正确加载 跳转也完美实现!
昨天买了一个服务器想着用来测试一些自己的项目,由于是第一次建站,在tomcat,linux,node.js间想了好久.最终因为node搭建比较方便没那么麻烦就决定用node.js来搭建网站项目. 搭建 ...
- linux下安装R
下载 版本:R3.2.2 下载地址:http://cran.rstudio.com/src/base/R-3/R-3.2.2.tar.gz 安装步骤: 解压 tar -zxvf R-3.2.2.tar ...
- MyOD C语言代码实现
Myod C语言实现 一.题目要求 1 复习c文件处理内容 2 编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能 main与其他分开,制作静态库和动态库 编写Ma ...
- DataV数据可视化功能特性
使用DataV制作实时销售数据可视化大屏 (本课程可以帮助数据分析师学习数据可视化大屏的制作,包括制作的方法.设计原则等基础知识,并提供一个微项目,使用数加的DataV基于ABC公司的经营数据,快速构 ...
- legend3---lavarel常用artisan命令操作
legend3---lavarel常用artisan命令操作 一.总结 一句话总结: 帮助:php artisan可以调出帮助命令 1.npm安装后盾js? npm install hdjs node ...
- 查看Linux基本系统信息
#! /bin/bash #The scripts will return the system infomation #return hostname and version infomation ...
- 谈一谈 Android 的安全机制?
1.Android 是基于 Linux 内核的,因此 Linux 对文件权限的控制同样适用于 Android,在 Android 中每个应用都有自己的/data/data/包名 文件夹,该文件夹只能该 ...