Spring初学之bean的生命周期
实体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的生命周期的更多相关文章
- (转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...
- Spring 容器中 Bean 的生命周期
Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...
- (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期
日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...
- Spring 学习笔记---Bean的生命周期
生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...
- Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)
Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...
- Spring实战(二)Spring容器和bean的生命周期
引入问题: 在XML配置文件中配置bean后,这些文件又是如何被加载的?它们被加载到哪里去了? Spring容器——框架核心 1.什么是Spring容器?它的功能是什么? 在基于Spring的应用中, ...
- Spring基础14——Bean的生命周期
1.IOC容器中的Bean的生命周期方法 SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务.SpringIOC容器对Bean的生命周期进行管理 ...
- spring(二):bean的生命周期
bean的生命周期指的是bean的创建——>初始化——>销毁的过程,该过程是由spring容器进行管理的 我们可以自定义bean初始化和销毁的方法:容器在bean进行到当前生命周期时,调用 ...
- IoC基础篇(一)--- Spring容器中Bean的生命周期
日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...
随机推荐
- oracelp---随意 记录(nvl)
1.Oracle的Nvl函数 nvl( ) 函数 从两个表达式返回一个非null 值. 语法 NVL(eExpression1, eExpression2) 参数 eExpression1, eExp ...
- asp.net mvc4连接mysql
环境:vs2013+mysql5.6+mysql connector for .net 6.8.3+MySQL for Visual Studio 1.1.3 参考:http://dev.mysql. ...
- C#反射Assembly 详细说明(转)
1.对C#反射机制的理解2.概念理解后,必须找到方法去完成,给出管理的主要语法3.最终给出实用的例子,反射出来dll中的方法 反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等 ...
- 滚动标签marquee
- python通过数据库连接池实现mysql数据库增删改查
import pymysql from DBUtils.PooledDB import PooledDB class SQLHandler(object): def __init__(self, ho ...
- Google Cloud Platfrom中使用Linux VM
Linkes https://cloud.google.com/compute/docs/quickstart-linuxhttps://console.cloud.google.com/comput ...
- 2015.6.30 反弹的教训(想做T)
心路:在6.29号,市场连续大跌!我到6.29号才想到可以做T+0.6.30消息面已经利好(双降准),已经计划做T+0(X先买后卖). 开市大跌至跌停.午后所有股票开始反弹.但是上午跌停时不敢入市, ...
- windows下客户端开发hdf--环境搭建
1.引入依赖 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop- ...
- PAT 天梯赛 L2-024. 部落 【并查集】
题目链接 https://www.patest.cn/contests/gplt/L2-024 题意 给出 几个不同的圈子,然后 判断 有哪些人 是属于同一个部落的,或者理解为 ,有哪些人 是有关系的 ...
- VM and Docker Container
https://www.zhihu.com/question/48174633 在开始讨论前,先抛出一些问题,可先别急着查看答案,讨论的过程可以让答案更有趣,问题如下: Docker 容器有自己的ke ...