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虚拟机对 ...
随机推荐
- Github上最受关注的前端大牛,快来膜拜吧!
1. Paul Irish Github主页: https://github.com/paulirish 个人主页: http://paulirish.com 维基百科: http://en.wiki ...
- h.264 Mode Decision
Mode Decision(模式选择)决定一个宏块以何种类型进行分割.宏块的分割类型有以下几种: //P_Skip and B_Skip means that nothing need to be e ...
- 一张图看懂片式多层陶瓷电容器(MLCC)
- VitamioBundle-master
1. 下载资源 (1) 核心插件 VitamioBundle 下载地址:https://github.com/yixia/VitamioBundle (2) 官方示例 VitamioDemo 下载地址 ...
- POJ 1287 Networking
题目链接: poj.org/problem?id=1287 题目大意: 你被分派到去设计一个区域的连接点,给出你每个点对之间的路线,你需要算出连接所有点路线的总长度. 题目输入: 一个数字n 代表有 ...
- perl中执行linux命令,及其区别
1. system("date '+%Y-%m-%d %H:%M:%S'") 该命令返回的是-1.(应该是date命令的main函数的返回值) 2. `date '+%Y-%m-% ...
- C#中枚举类型和int类型的转化
先定义一个枚举类型 , 初中, 高中,大学 }; int ->enum int d=2; PropertyType a=(PropertyType)d; int <- enum Prop ...
- 3D视频可能出现的质量问题 (MSU出品)
俄罗斯的MSU Graphics & Media Lab (Video Group)提出的3D视频存在的几种问题.有一定的参考价值,在此翻译一下. 他们目前总结出4种问题: 水平视差(Hori ...
- 搭建完整邮件系统(postfix+dovecot+clamAV+Spamassassin+amavisd-new)
============================ 相关软件: 1. 发送邮件 --- postfix 2. 身份认证 --- sasl2 3. 接收邮件 --- dovecot 4. 防病毒邮 ...
- hash表的创建
功能:创建一个hash table.假设有处理冲突,则採用再散列法放置该元素 代码參考<零基础学数据结构> 代码例如以下: root@ubuntu:/mnt/shared/appbox/h ...