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的生命周期的更多相关文章

  1. spring in action 学习笔记四:bean的生命周期

    bean 的生命周期分为:一个是ApplicationContext的容器的bean的生命周期,另一个是BeanFactory容器的生命周期. 首先介绍一下:ApplicationContext的容器 ...

  2. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring Bean的生命周期

    Spring 容器可以管理 singleton 作用域 Bean 的生命周期,在此作用域下,Spring 能够精确地知道该 Bean 何时被创建,何时初始化完成,以及何时被销毁. 而对于 protot ...

  3. Spring学习-- IOC 容器中 bean 的生命周期

    Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...

  4. 管理Bean的生命周期

    [IOC容器中Bean的生命周期方法] 1.SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2.Spring IOC容器对Bean的生命周 ...

  5. 【Spring注解驱动开发】使用InitializingBean和DisposableBean来管理bean的生命周期,你真的了解吗?

    写在前面 在<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!>一文中,我们讲述了如何使用@Bean注解来指定bean初始化和销毁的方法.具体的 ...

  6. Spring_管理bean的生命周期

    Spring IOC 容器对 Bean 的生命周期进行管理的过程:通过构造器或工厂方法创建 Bean 实例为 Bean 的属性设置值和对其他 Bean 的引用将 Bean 实例传递给 Bean 后置处 ...

  7. 好记性不如烂笔头86-spring3学习(7)-ApplicationContext中bean的生命周期

    假设使用ApplicationContext来生成.管理Bean, 一个Bean从建立到销毁,会历经几个运行阶段. 我个人理解一般的bean的生命周期主要包含:建立,初始化,使用阶段,销毁四个核心阶段 ...

  8. maven权威指南学习笔记(四)—— maven生命周期(lifecycle)

    定义: 生命周期是包含在一个项目构建中的一系列有序的阶段 举个例子来说就是maven 对一个工程进行: 验证(validate) -- 编译源码(compile) -- 编译测试源码(test-com ...

  9. (转)《深入理解java虚拟机》学习笔记7——Java虚拟机类生命周期

    C/C++等纯编译语言从源码到最终执行一般要经历:编译.连接和运行三个阶段,连接是在编译期间完成,而java在编译期间仅仅是将源码编译为Java虚拟机可以识别的字节码Class类文件,Java虚拟机对 ...

随机推荐

  1. LVS+PIRANHA测试

    有一个知识盲点,是这个VIP,在四个服务器上都要设置? 现在只测试了两个机器,REAL SERVER没有开动. 如果LVS和REALSERVER都可以停一个作互切的话,那KEEPALIVED要它他什么 ...

  2. 多备份CEO胡茂华:创业路上的五道坎

    本文由多备份CEO胡茂华记述,授权南七道发表,未做删改.胡茂华:腾讯第116号员工,历任腾讯总监.盛大CTO (旅游).1号店技术副总裁.现担任云服务提供商多备份联合创始人&CEO. 2014 ...

  3. Reverse Linked List II——LeetCode

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  4. 铁通、长宽网络支付时“签名失败”问题分析及解决方案  [88222001]验证签名异常:FAIL[20131101100002-142]

    原文地址:http://bbs.tenpay.com/forum.php?mod=viewthread&tid=13723&highlight=%CC%FA%CD%A8 如果你的是铁通 ...

  5. has-a关系——包含对象成员的类

    #ifndef _STUDENT_H_ #define _STUDENT_H_ #include <iostream> #include <string> #include & ...

  6. STL——heap的4大操作

    STL的堆操作 STL里面的堆操作一般用到的只有4个:make_heap();.pop_heap();.push_heap();.sort_heap(); 他们的头文件函数是#include < ...

  7. 使用XSLT实现Word下载

    Xslt是Extensible Stylesheet Language Transformations的缩写,用来将XML 文档转换到其它文档类型.XSLT的使用包括两个输入文件: – 包含实际数据的 ...

  8. Hbase设计实战

    Hbase设计实战 本文通过一个游戏公司客户实际案例的讲解,分析了 Hbase 表设计及开发在实际案例中的运用,对比了不同的 Hbase 设计考量对客户端访问模式及检索性能的差异.读者通过案例中 Hb ...

  9. 启动android默认浏览器

    一.启动android默认浏览器 Intent intent = new Intent();         intent.setAction("android.intent.action. ...

  10. [RxJS] Creation operator: create()

    We have been using Observable.create() a lot in previous lessons, so let's take a closer look how do ...