Spring点滴四:Spring Bean生命周期
Spring Bean 生命周期示意图:
了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程。
---------------------------------------------------------------------------------------------------------------------------------------------------
spring-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 定义一个bean -->
<bean id="narCodeService" class="com.test.service.impl.NarCodeServiceImpl">
</bean>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="beanLifecycle" class="com.test.spring.BeanLifecycle" init-method="init" destroy-method="close">
<property name="name" value="张三"></property>
<property name="sex" value="男"></property>
</bean>
<bean id="postProcessor" class="com.test.spring.PostProcessor"/>
</beans>
Service Class:
package com.test.spring; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata; import com.sun.org.apache.xml.internal.security.Init; /**
* 测试Spring Bean的生命周期
* @author zss
*
*/
public class BeanLifecycle implements InitializingBean,DisposableBean
,BeanFactoryAware,BeanNameAware,ApplicationContextAware{
private String name;
private String sex; public String getName() {
return name;
} public void setName(String name) {
System.out.println("》》》调用BeanLifecycle对象"+this.getName()+"属性set方法,设值为:"+name);
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
System.out.println("》》》调用BeanLifecycle对象"+this.getSex()+"属性set方法,设值为:"+sex);
this.sex = sex;
}
/**
* 依次开始调用初始化回调方法
*/
@PostConstruct
public void initPostConstruct(){
System.out.println("》》》注解初始化方法被调用");
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("》》》BeanLifecycle调用了InitailizingBean的afterPorpertiesSet方法了.....");
} public void init (){
System.out.println("》》》init方法被调用");
} /**
* 依次开始调用销毁回调方法
*/ @PreDestroy
public void preDestroy(){
System.out.println("》》》注解销毁方法被调用");
} @Override
public void destroy() throws Exception {
System.out.println("》》》BeanLifecycle从Spring IoC容器中移除了.......");
}
public void close() {
System.out.println("》》》close方法被调用");
} @Override
public void setApplicationContext(ApplicationContext paramApplicationContext)
throws BeansException {
System.out.print("》》》调用ApplicationContextAware接口setApplicationContext方法:");
System.out.println(paramApplicationContext); } // @Override
// public void setResourceLoader(ResourceLoader paramResourceLoader) {
// System.out.print("》》》调用ResourceLoaderAware接口setResourceLoader方法:");
// System.out.println(paramResourceLoader);
// } // @Override
// public void setImportMetadata(AnnotationMetadata paramAnnotationMetadata) {
// System.out.println(333333);
// } // @Override
// public void setEnvironment(Environment paramEnvironment) {
// System.out.print("》》》调用EnvironmentAware接口setEnvironment方法:");
// System.out.println(paramEnvironment);
// } @Override
public void setBeanName(String paramString) {
System.out.println("》》》调用BeanNameAware接口setBenaName方法: "+paramString); } @Override
public void setBeanFactory(BeanFactory paramBeanFactory)
throws BeansException { System.out.print("》》》调用BeanFactoryAware接口setBeanFactory方法:");
System.out.println(paramBeanFactory); } // @Override
// public void setBeanClassLoader(ClassLoader paramClassLoader) {
// System.out.print("》》》调用BeanClassLoaderAware接口setBeanClassLoader方法:");
// System.out.println(paramClassLoader);
// } // @Override
// public void setApplicationEventPublisher(
// ApplicationEventPublisher paramApplicationEventPublisher) {
// System.out.print("》》》调用ApplicationEventPublisherAware接口setApplicationEventPublisher方法:");
// System.out.println(paramApplicationEventPublisher);
// } @Override
public String toString() {
return "BeanLifecycle [name=" + name + ", sex=" + sex + "]";
} }
Test:
package com.test.spring; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
ClassPathXmlApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class);
//applicationcontext.close();
applicationcontext.registerShutdownHook();
}
}
测试结果:
》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 00:08:01 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6c7e6b26: startup date [Sun Mar 19 00:08:01 CST 2017]; root of context hierarchy
2017-03-19 00:08:01 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》调用BeanLifecycle对象null属性set方法,设值为:张三
》》》调用BeanLifecycle对象null属性set方法,设值为:男
》》》调用BeanNameAware接口setBenaName方法: beanLifecycle
》》》调用BeanFactoryAware接口setBeanFactory方法:org.springframework.beans.factory.support.DefaultListableBeanFactory@75a6bd5: defining beans [narCodeService,org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,beanLifecycle,postProcessor]; root of factory hierarchy
》》》调用ApplicationContextAware接口setApplicationContext方法:org.springframework.context.support.ClassPathXmlApplicationContext@6c7e6b26: startup date [Sun Mar 19 00:08:01 CST 2017]; root of context hierarchy
后置处理器处理bean=【beanLifecycle】开始
》》》注解初始化方法被调用
BeanLifecycle [name=张三, sex=男]
》》》BeanLifecycle调用了InitailizingBean的afterPorpertiesSet方法了.....
BeanLifecycle [name=张三, sex=男]
》》》init方法被调用
后置处理器处理bean=【beanLifecycle】完毕!
》》》Spring ApplicationContext容器初始化完毕了......
2017-03-19 00:08:03 INFO:ClassPathXmlApplicationContext-Closing org.springframework.context.support.ClassPathXmlApplicationContext@6c7e6b26: startup date [Sun Mar 19 00:08:01 CST 2017]; root of context hierarchy
》》》注解销毁方法被调用
》》》BeanLifecycle从Spring IoC容器中移除了.......
》》》close方法被调用
关于Spring BeanPostProcessor(后置处理器)讲解请查看:http://www.cnblogs.com/sishang/p/6576665.html
Spring点滴四:Spring Bean生命周期的更多相关文章
- Spring(四)之Bean生命周期、BeanPost处理
一.Bean 生命周期 Spring bean的生命周期很容易理解.当bean被实例化时,可能需要执行一些初始化以使其进入可用状态.类似地,当不再需要bean并从容器中移除bean时,可能需要进行一些 ...
- Spring源码系列 — Bean生命周期
前言 上篇文章中介绍了Spring容器的扩展点,这个是在Bean的创建过程之前执行的逻辑.承接扩展点之后,就是Spring容器的另一个核心:Bean的生命周期过程.这个生命周期过程大致经历了一下的几个 ...
- Spring之BeanFactory及Bean生命周期
1.spring通过BeanFactory灵活配置.管理bean,Spring对管理的bean没有任何特别的要求,完全支持对POJO的管理: 2.BeanFactory有个ApplicationCon ...
- Spring源码 21 Bean生命周期
参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...
- Spring BeanFactory 初始化 和 Bean 生命周期
(version:spring-context-4.3.15.RELEASE) AbstractApplicationContext#refresh() public void refresh() t ...
- Spring源码之Bean生命周期
https://www.jianshu.com/p/1dec08d290c1 https://www.cnblogs.com/zrtqsk/p/3735273.html 总结 将class文件加载成B ...
- 【不懂】spring bean生命周期
完整的生命周期(牢记): 1.spring容器准备 2.实例化bean 3.注入依赖关系 4.初始化bean 5.使用bean 6.销毁bean Bean的完整生命週期可以認為是從容器建立初始化Bea ...
- 金三银四,还在为spring源码发愁吗?bean生命周期,看了这篇就够了
第一,这绝对是一个面试高频题. 比第一还重要的第二,这绝对是一个让人爱恨交加的面试题.为什么这么说?我觉得可以从三个方面来说: 先说会不会.看过源码的人,这个不难:没看过源码的人,无论是学.硬背.还是 ...
- spring(二、bean生命周期、用到的设计模式、常用注解)
spring(二.bean生命周期.用到的设计模式.常用注解) Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的. ...
随机推荐
- linux镜像(持续更新)
Linux系统历史衍生图:https://upload.wikimedia.org/wikipedia/commons/1/1b/Linux_Distribution_Timeline.svg ubu ...
- Docker 入门之docker容器创建
使用docker容器的大多数人都是因为想要隔离不同运行环境的差异,使得自己的应用能更好的移植和部署.那么我们来看看掌握docker需要掌握哪些方面. 1,搭建docker环境 2,编译镜像并将其运行成 ...
- Redis勒索事件爆发,如何避免从删库到跑路?
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云数据库 TencentDB发表于云+社区专栏 9月10日下午,又一起规模化利用Redis未授权访问漏洞攻击数据库的事件发生,此次 ...
- docker 部署 zookeeper+kafka 集群
主机三台172.16.100.61172.16.100.62172.16.100.63Docker 版本 当前最新版 # 部署zk有2种方法 ## 注意 \后不要跟空格 一 . 端口映射 172.16 ...
- 从零开始的Python学习Episode 21——socket基础
socket基础 网络通信要素: A:IP地址 (1) 用来标识网络上一台独立的主机 (2) IP地址 = 网络地址 + 主机地址(网络号:用于识别主机所在的网络/网段.主机号:用于识别该网络中的 ...
- FileZilla-FTP连接失败
状态: 已登录状态: 读取“/”的目录列表...命令: CWD /响应: 250 CWD successful. "/" is current directory.命令: TYPE ...
- Python 中的一些小技巧
这里是本人收集的一些 Python 小技巧,目前主要是一些实用函数,适合有一定基础的童鞋观看(不会专门介绍使用到的标准库函数).. 一.函数式编程 函数式编程用来处理数据,感觉很方便.(要是再配上管道 ...
- sublime c/c++ 环境
sublime c/c++ 环境 参考: 别人的教程1 别人的教程2 注意,一定要用cmd先试一下,编译成功后再用sublime试 我遇到了一个很诡异的问题,就是cmd运行正常但sublime显示 在 ...
- “Hello World!”团队第六周的第三次会议
今天是我们团队“Hello World!”团队第六周召开的第三次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...
- 【Alpha】技术规格说明书
由于第1周已经写过技术规格说明书(设计文档),本周更新了上一版内容. Github地址:https://github.com/buaase/Phylab-Web/blob/master/docs/Ba ...