深入理解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官方 ...
随机推荐
- App开发外包必须注意的四大骗局
在app外包过程中有很多需要注意的事项,今天专门挑选注意事项中的"骗局"这个话题来与大家分享一些的常见骗局及其细节. 无论是从新闻还是身边的朋友,我们都经常可以听见"xx ...
- matlab中小技巧
关于matlab中可能遇到的小知识点 一.字符串的比较 不能使用“==”,需要使用函数strcmp() %matlab中字符串的比较 %字符串比较要用strcmp.相同则返回1,不相同则返回0. cl ...
- java线程之多个生产者消费者2.0
上一节中,通过while和notifyAll解决了多个生产者,消费者对共享资源的访问问题,现在开始升级 但是,仍然有改进之处,主要体现在两点: 1)使用新版本1.5开始后的锁Lock解决,目的将其全部 ...
- OSS项目进度(2.19)
前两周先后完成了OSS.Common ,OSS.Http ,OSS.Social 项目的.net standard支持,再次开始进入框架本身的开发,今天进度情况: 一.OSS.Social 完成摇一摇 ...
- APC注入
0X01 注入原理 当线程被唤醒时APC中的注册函数会被执行的机制,并依此去调用我们的DLL加载代码,进而完成注入的目的 具体的流程: 1 当EXE里的某个线程执行到sleepEX(),或者waitF ...
- #define宏与const的区别
1.#define宏的用法 #define用宏名代替一个字符串,这样便于修改,提高了程序的可移植性.编译器在编译预处理时只对宏做文本替换,而不进行类型检查,所以替换后可能产生一些副作用. 带参数的宏类 ...
- lamp论坛搭建
1.配置本地yum源 mount 2.安装软件 yum install httpd mariadb mariadb-server php php-mysql php-gd libjpeg* ...
- Ubuntu 小白安装血泪史
介绍: 新入手的Ubuntu:版本 命令行模式下 出现 ♦♦♦♦ 图形界面无法获取最高权限:gurb.cfg 无法再图形界面下修改 安装类型: 1.安装Ubuntu,与Window 7共存 2.清除整 ...
- 禁止Linux系统被 ping
echo "net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf sysctl -p 生效 开启ping功能: 删除 ...
- JavaScript中的文档模式和严格模式
JavaScript中的文档模式和严格模式 语法模式有普通模式和严格模式两种 普通模式:正常的JavaScript语法拼写以及代码编写(相对于严格模式存在着语法上的不严谨),尽可能的识别错误以及不规范 ...