Spring4.0学习笔记(5) —— 管理bean的生命周期
Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务
Spring IOC 容器对Bean的生命周期进行管理的过程:
1、通过构造器或工厂方法创建Bean的实例
2、为Bean的属性设置值和对其他Bean的引用
3、调用Bean的初始化方法
4、Bean可以使用了
5、当容器关闭时,调用Bean的销毁方法
bean文件
package com.spring.cycle;
public class Car {
public Car(){
System.out.println("Car's constructor...");
}
@Override
public String toString() {
return "Car [brand=" + brand + "]";
}
private String brand;
public void setBrand(String brand){
System.out.println("setBrand...");
this.brand = brand;
}
public void init(){
System.out.println("init...");
}
public void destroy(){
System.out.println("destroy...");
}
}
配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="car" class="com.spring.cycle.Car" init-method="init" destroy-method="destroy">
<property name="brand" value="Audi"></property>
</bean> <bean class="com.spring.cycle.MyBeanPostProcesser">
</bean>
</beans>
package com.spring.cycle;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bean-cycle.xml");
Car car = (Car)ctx.getBean("car");
System.out.println(car);
//关闭IOC容器
ctx.close();
}
}
postProcessAfterInitialization 和 postProcessBeforeInitialization
package com.spring.cycle; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcesser implements BeanPostProcessor { public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessAfterInitialization"+arg1);
return arg0;
} public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("postProcessBeforeInitialization"+arg1);
return arg0;
} }
bean的生命周期
Car's constructor... 【调用构造器】
setBrand... 【设置属性】
postProcessBeforeInitializationcar 【实例化方法前】
init...【实例化bean】
postProcessAfterInitializationcar 【实例化方法后】
Car [brand=Audi] 【调用toString()方法】
destroy... 【销毁bean】
Spring4.0学习笔记(5) —— 管理bean的生命周期的更多相关文章
- spring in action 学习笔记四:bean的生命周期
bean 的生命周期分为:一个是ApplicationContext的容器的bean的生命周期,另一个是BeanFactory容器的生命周期. 首先介绍一下:ApplicationContext的容器 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring Bean的生命周期
Spring 容器可以管理 singleton 作用域 Bean 的生命周期,在此作用域下,Spring 能够精确地知道该 Bean 何时被创建,何时初始化完成,以及何时被销毁. 而对于 protot ...
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- 管理Bean的生命周期
[IOC容器中Bean的生命周期方法] 1.SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2.Spring IOC容器对Bean的生命周 ...
- 【Spring注解驱动开发】使用InitializingBean和DisposableBean来管理bean的生命周期,你真的了解吗?
写在前面 在<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!>一文中,我们讲述了如何使用@Bean注解来指定bean初始化和销毁的方法.具体的 ...
- Spring_管理bean的生命周期
Spring IOC 容器对 Bean 的生命周期进行管理的过程:通过构造器或工厂方法创建 Bean 实例为 Bean 的属性设置值和对其他 Bean 的引用将 Bean 实例传递给 Bean 后置处 ...
- 好记性不如烂笔头86-spring3学习(7)-ApplicationContext中bean的生命周期
假设使用ApplicationContext来生成.管理Bean, 一个Bean从建立到销毁,会历经几个运行阶段. 我个人理解一般的bean的生命周期主要包含:建立,初始化,使用阶段,销毁四个核心阶段 ...
- maven权威指南学习笔记(四)—— maven生命周期(lifecycle)
定义: 生命周期是包含在一个项目构建中的一系列有序的阶段 举个例子来说就是maven 对一个工程进行: 验证(validate) -- 编译源码(compile) -- 编译测试源码(test-com ...
- (转)《深入理解java虚拟机》学习笔记7——Java虚拟机类生命周期
C/C++等纯编译语言从源码到最终执行一般要经历:编译.连接和运行三个阶段,连接是在编译期间完成,而java在编译期间仅仅是将源码编译为Java虚拟机可以识别的字节码Class类文件,Java虚拟机对 ...
随机推荐
- OSSEC
[科普]入侵检测系统ossec配置文件详解 http://www.freebuf.com/articles/system/11862.html http://www.freebuf.com/autho ...
- PLSQL死循环
begin loop insert into for1 values('a'); commit; end loop; end;
- 【HDOJ】5131 Song Jiang's rank list
STL的使用. /* 5131 */ #include <iostream> #include <map> #include <cstdio> #include & ...
- Graph Search图谱搜索
来自百度百科的解释: Graph Search为2013年1月16日,Facebook首席执行官马克·扎克伯格(Mark Zuckerberg)在门罗帕克公司总部召开的新闻发布会上宣布推出社交搜索工具 ...
- .NET开发者需要的工具箱
本文作者 Spencer 是一名专注于 ASP.NET 和 C# 的程序员,他列举了平时工作.在家所使用的大部分开发工具,其中大部分工具都是集中于开发,当然也有一些其它用途的,比如图片处理.文件压缩等 ...
- Wii硬盘游戏管理
简介 wuyou是我心目中的Wii硬盘游戏管理神器,没有之一. wuyou不仅仅是一个游戏管理软件,它最亮点的地方在于集成了“游戏文件格式的转换功能”,电玩巴士上如是说: 用Wii主机来把游戏复制到硬 ...
- 《算法问题实战策略》-chaper17-部分和
数组上的一个基本优化——部分和: 对于一定长度的数组,我们想不断访问这个数组上的某个区间的和,我们能够怎么做呢?这里先不去谈一些数据结构在这个问题上的优化处理.首先我们最简单的一个方法就是穷举出所有区 ...
- AES - Rijndael 算法(一)
1997年1月,美国标准技术协会NIST开始遴选数据加密标准(Data Encryption Standard,简称DES)替代者的工作,称为高级加密标准[1’2](Advanced Enerypti ...
- java中集合杂记
HashSet类按照哈希算法来存取集合中的对象,具有很有的性能.当HashSet向集合中加入一个对象时,会调用对象的hashCode()方法获得哈希码,然后根据这个哈希码进一步计算出对象在集合中的存放 ...
- python:unittest(测试框架)
一,基本概念 TestCase:所有测试用例的基类 TestLoad:加载测试用例,返回TestSuite(测试套件) TestSuite:创建测试套件 TextTestRunner:运行测试用例 T ...