实体Bean:

Car.java:

package spring.beans.cycle;

public class Car {
private String name;
private int price; public Car() {
super();
System.out.println("Constructor...");
} public void init(){//init-method
System.out.println("init...");
}
public void destroy(){//destroy-method
System.out.println("destroy...");
} public String getName() {
return name;
}
public void setName(String name) {
System.out.println("setName...");
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
System.out.println("setPrice...");
this.price = price;
} @Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]";
} }

Spring的配置文件核心代码添加:

    <bean id="car" class="spring.beans.cycle.Car"
p:name="奥迪" p:price="300000"
init-method="init"
destroy-method="destroy"> </bean>

实现BeanPostProcessor接口,并具体提供两个方法的实现
postProcessAfterInitialization:init-method方法之后调用,
postProcessBeforeInitialization:init-method方法之前调用,
两个参数:
bean:bean实例本身
beanName:IOC容器配置的bean的名字
返回值:是实际上返回给用户的bean,注意:可以在上面两个方法中修改bean,甚至返回一个新的bean。

MyBeanPostProcessor.java:

package spring.beans.cycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization"+bean+","+beanName); if("...".equals(beanName)){
//...
} Car car=new Car();
car.setName("福特"); return car;
} @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization"+bean+","+beanName); Car car=new Car();
car.setName("长安"); return car;
} }

把这个类配置到spring容器中:

<!-- 配置bean的后置处理器 -->
<bean class="spring.beans.cycle.MyBeanPostProcessor"></bean>

测试方法:

package spring.beans.cycle.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import spring.beans.cycle.Car;

public class Main {

    public static void main(String[] args) {
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans-cycle.xml"); Car car=(Car) ctx.getBean("car");
System.out.println(car); ctx.close();
} }

输出:

Constructor...
setName...
setPrice...
postProcessBeforeInitializationCar [name=奥迪, price=300000],car
Constructor...
setName...
init...
postProcessAfterInitializationCar [name=长安, price=0],car
Constructor...
setName...
Car [name=福特, price=0]
四月 13, 2017 3:46:13 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@3f91beef: startup date [Thu Apr 13 15:46:13 CST 2017]; root of context hierarchy
destroy...

Spring初学之bean的生命周期的更多相关文章

  1. (转)Spring管理的Bean的生命周期

    http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...

  2. Spring 容器中 Bean 的生命周期

    Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...

  3. (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  4. Spring 学习笔记---Bean的生命周期

    生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...

  5. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

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

  6. Spring实战(二)Spring容器和bean的生命周期

    引入问题: 在XML配置文件中配置bean后,这些文件又是如何被加载的?它们被加载到哪里去了? Spring容器——框架核心 1.什么是Spring容器?它的功能是什么? 在基于Spring的应用中, ...

  7. Spring基础14——Bean的生命周期

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

  8. spring(二):bean的生命周期

    bean的生命周期指的是bean的创建——>初始化——>销毁的过程,该过程是由spring容器进行管理的 我们可以自定义bean初始化和销毁的方法:容器在bean进行到当前生命周期时,调用 ...

  9. IoC基础篇(一)--- Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

随机推荐

  1. 【BZOJ1812】[Ioi2005]riv 树形DP

    [BZOJ1812][Ioi2005]riv Description 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河, ...

  2. 《从零开始学Swift》学习笔记(Day4)——用Playground工具编写Swift

    Swift 2.0学习笔记(Day4)——用Playground工具编写Swift 原创文章,欢迎转载.转载请注明:关东升的博客 用Playground编写Swift代码目的是为了学习.测试算法.验证 ...

  3. 160816、webpack 入门指南

    什么是 webpack? webpack是近期最火的一款模块加载器兼打包工具,它能把各种资源,例如JS(含JSX).coffee.样式(含less/sass).图片等都作为模块来使用和处理. 我们可以 ...

  4. [Node.js] require背后的故事

    前言 熟悉Node.js的肯定对下面的代码熟悉 var http = require('http'); 这段代码很好理解,就是加载一个http模块.但是你有没有想过为什么要这么写?这其中的缘由是什么呢 ...

  5. 基于网页api(接口)实现查快递

    之前在网上找到一款下载某慕课网站的java版软件,我想知道他是怎么实现:对于视频的下载的,毕竟网页源码中大都不会直接放视频的地址,但是没有公布源码,我就反编译,等到了部分“源码”,逻辑上还是有些问题, ...

  6. 【转】通过VIOS实现AIX系统的网络虚拟化

    在上一篇博文中,我们已经在一个新创建的LPAR中通过File-backed device以及VMLibrary的方式成功安装了一个AIX系统,接下来我们讨论如何通过VIOS的协助来完成新装AIX系统的 ...

  7. where VS having

    where 和 having 的区别:        WHERE 子句不能包含聚集函数: 因为试图用聚集函数判断那些行输入给聚集运算是没有意义的.相反,HAVING 子句总是包含聚集函数    hav ...

  8. 二值法方法综述及matlab程序

    在某些图像处理当中一个关键步是二值法,二值化一方面能够去除冗余信息,另一方面也会使有效信息丢失.所以有效的二值化算法是后续的处理的基础.比如对于想要最大限度的保留下面图的中文字,以便后续的定位处理. ...

  9. 20170520 BADI增强学习

    一.要求:Tcode:FF_5 导入数据运行时,产生财务凭证之前修改某些字段值.Exmp:FEBRE-VWEZWBKPF-XBLNRFEBEP-CHECTBSEG-ZUONR there is a b ...

  10. LeetCode-day01&02

    感觉还好,坚持住就行,毕竟智商不够 1. Length of Last Word求一个数组的最后一个单词的长度 2. Plus One   大数加1 3.  Add Binary 二进制加法 4. S ...