深入理解Spring中bean的生命周期
[Spring中bean的生命周期]

bean的生命周期
1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期:
(1).生命周期图:

(2).具体事例:
person类实现BeanNameAware,BeanFactoryAware接口
public class Person implements BeanNameAware ,BeanFactoryAware{
private String name;
public Person(){
System.out.println("调用构造器为属性值初始化");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
System.out.println("获取beanName id值"+" "+arg0);
}
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("获取BeanFactory" +" "+arg0);
}
}
public class MyBeanPostProcessor implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("调用postProcessAfterInitialization");
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("调用postProcessBeforeInitialization");
return arg0;
}
}
ApplicationContext.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="person" class="org.jingdong.bean.life.Person">
<property name="name" value="grl"></property>
</bean> <bean id="myBeanPostProcessor" class="org.jingdong.bean.life.MyBeanPostProcessor"></bean>
</beans>
Main.java
public class Main {
public static void main(String[] args) {
// 创建IOC容器
ApplicationContext ac = new ClassPathXmlApplicationContext("org/jingdong/bean/life/applicationContext.xml");
//从容器中获取bean实例
Person person = (Person) ac.getBean("person");
//使用bean
System.out.println(person.getName());
}
}
2.以Spring Factory装配bean为例:
(1).生命周期图:

深入理解Spring中bean的生命周期的更多相关文章
- 通过BeanPostProcessor理解Spring中Bean的生命周期
通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...
- 如果你每次面试前都要去背一篇Spring中Bean的生命周期,请看完这篇文章
前言 当你准备去复习Spring中Bean的生命周期的时候,这个时候你开始上网找资料,很大概率会看到下面这张图: 先不论这张图上是否全面,但是就说这张图吧,你是不是背了又忘,忘了又背? 究其原因在于, ...
- 一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今生
前言 在<spring中FactoryBean是什么bean>一文中,带着小伙伴学习了spring中的FactoryBean,了解了到了FactoryBean其实是一种生产Bean的bea ...
- JAVA面试题:Spring中bean的生命周期
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...
- Spring中Bean的生命周期及其扩展点
原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...
- 简:Spring中Bean的生命周期及代码示例
(重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...
- 一分钟掌握Spring中bean的生命周期!
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean 的别名只能维持 ...
- Spring中bean的生命周期!
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...
- Spring中 bean的生命周期
为什么要了解Spring中 bean的生命周期? 有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要. 二话不说先上图: 在谈具体流程之前先看看Spring官方 ...
随机推荐
- CSS3知识点整理(一)----基本样式
(一) 在编写CSS3样式时,不同的浏览器可能需要不同的前缀.它表示该CSS属性或规则尚未成为W3C标准的一部分,是浏览器的私有属性,虽然目前较新版本的浏览器都是不需要前缀的,但为了更好的向前兼容前缀 ...
- Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSInvocation setArgument:atIndex:]: index (3) out of bounds [-1, 2]'
这是相机调用方法的时候参数错误
- css3绘制腾讯logo
CSS3绘制的腾讯LOGO,下边是对比图. 演示地址
- 初探 discuz
测试: vim /etc/hosts ##ip地址转换 修改windows 的配置文件,写字板打开 vim /usr/local/apache/conf/httpd.conf vim /u ...
- C#中字符和字符串总结
Char类是C#提供的字符类型,String是C#提供的字符串类型. 字符: Char类在C#中表示一个Unicode字符. Char类只定义一个Unicode字符. Char类常用的方法及说明如下: ...
- ajax实现下载功能
ajax实现下载功能 适用场景:由于点击按钮下载excel响应时间过长,此时间段加入加载样式(灰色层.加载动画): 浏览器弹出下载框后,上面的加载样式去掉. 方 法 :使用jquery.fi ...
- 关于C语言中变量类型转换
今天在工作中遇到一个问题,而在解决问题的过程中,发现一段关于int 型变量(a)和char型(b)变量间类型转换的代码存在问题:一个值为255的int型变量a,强制类型转换并赋值给char型变量b后, ...
- STL源码剖析(读书笔记)
STL迭代器种类 2. 迭代器型别使用范例: 3. SGI STL空间配置器分为两级: 4. Vector 的内部存储方式为数组,随机访问迭代器. 5. Vector的size获取方式: 6. Vec ...
- Java编程风格学习(三)
在上一篇的java编程风格学习(二)中我们学习了一些在Java编码过程中的格式规范,遵循这些规范毋庸置疑是我们的书写高质量代码的前提与基础.今天我们更进一步,一起来学习Java编程的命名规范,向着编写 ...
- 阿里云服务器 发送邮箱 STMP 25端口 465端口问题 Javamail 25被禁用
我们传统使用的比较简单的是 STMP 25端口收发邮件 今天发现刚购买的阿里云服务器不能作为客户端通过STMP 25端口发送邮件 开始在网上有说发现是JDK1.8的原因,然后自己也把JDK1.8换到了 ...