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. 尽历磨难,搞定OPEN VSWITCH安装

    参考贴: http://sudomakeinstall.com/linux-systems/installing-openvswitch-on-centos-6-6-5 yum install ker ...

  2. Linux&shell之高级Shell脚本编程-创建菜单

    写在前面:案例.常用.归类.解释说明.(By Jim) 创建菜单#!/bin/bash# testing the scriptclearechoecho -e "\t\t\tSys Admi ...

  3. ExecuteNonQuery返回负数

    用 ExecuteNonQuery 执行sql语句"select * from table where id=@id"如果检索出符合条件的ID ExecuteNonQueue 会返 ...

  4. bzoj2730 [HNOI2012]矿场搭建 (UVAlive5135 Mining Your Own Business)

    2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1147  Solved: 528[Submit][Statu ...

  5. 通过百度获取IP地址对应的经纬度

    /** * 获取指定IP对应的经纬度(为空返回当前机器经纬度) *  * @param ip * @return */ public static String[] getIPXY(String ip ...

  6. css中居中的居中方法

    1:给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto; } 2:居中一个浮动元素 确定容器的宽高 宽500 高 300 的层 ...

  7. 【转】在Eclipse环境下配置Servlet开发环境

    配置这个真心坑···浪费我一下午时间,而且去网上找了一圈资料发现都不靠谱呀= = 于是自己写一个,防止忘记了 一.配置前准备 你需要下载三个东西 Eclipse J2EE:http://www.ecl ...

  8. Asp.Net异常:"由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值"的解决方法

    今天项目中碰到一个以前从没有见过的异常信息“由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值”,于是查了一下资料,原来此异常是由于我在代码中使用了"Response.End ...

  9. Chrome调试大全--转载

    作为一名前端开发者,打交道最多的可能是和浏览器.市面上各种浏览器多不胜数,主流的有Chrome,Firefox,Safari,IE,Opera,非主流的如360,遨游,QQ浏览器,搜狗浏览器,据说淘宝 ...

  10. Sql 语句添加字段、修改字段类型、默认值语法

    Sql 语句添加字段 ,) not null --修改类型 alter Table bbs ) Sql 语句修改默认值 alter table 表名 drop constraint 约束名字 --删除 ...