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也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...
随机推荐
- libnids介
转自:http://blog.chinaunix.net/uid-22832715-id-2111578.html Libnids开发包介绍 Libnids是一个用于网络入侵检测开发的专业编程 ...
- eclipse java文件提示 The import XXX cannot be resolved
问题:eclipse导入类 提示The import XXX cannot be resolved 原因:原来使用JDK和现在使用的JDK不同造成的buildpath不对 解決方法: 1.右键项目 ...
- 20160924-2——mysql常见问题集锦
一.数据类型相关问题 1.varchar(N)占用多少空间 (1)varchar(N)里的N是字符数,而不是字节数: (2)字符类型(varchar text blob等)空间=字符实际长度+字段长度 ...
- POJ 1789 Truck History【最小生成树简单应用】
链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- linux环境配置nginx导致页面不刷新
在linux环境下,配置了nginx负载均衡,由于可能在虚拟主机的配置文件nginx.conf中,对缓存机制未配置成功,导致页面不刷新,仍然显示缓存中的内容. 最后通过注释nginx.conf文件中的 ...
- Mac下最好用的文本编辑器
友情提醒:图多杀猫. 曾经在Windows下一直用gVim.能够用键盘控制一切,操作起来是又快又爽,还支持一大堆插件.想怎么玩就怎么玩.后来转Mac后,也沿袭着之前的习惯.一直在用终端的Vim.偶尔会 ...
- Linux用户相关文件之用户信息文件
1.文件地址: /etc/pssswd -rw-r--r--. 1 root root 936 10月 6 12:50 /etc/passwd 2.文件内容: xiaol_1:x:501:501::/ ...
- MySql存储过程、函数
存储过程和函数是在数据库中定义一些SQL语句的集合,然后直接调用这些存储过程和函数来执行已经定义好的SQL语句.存储过程和函数可以避免开发人员重复的编写相同的SQL语句.而且,存储过程和函数是在MyS ...
- 在一台服务器上搭建多个网站的方法(Apache版)
Apache的配置文件一般放置在/etc/httpd/conf文件夹下,httpd.conf是它的主配置文件,在进行配置时可以将虚拟主机的配置文件单独配置,如取名为vhost.conf,然后再http ...
- Debussy的安装与使用
1.概述 Debussy是NOVAS Software, Inc ( 思源科技 )发展的HDL Debug & Analysis tool,这套软体主要不是用来跑模拟或看波形,它最强大的功能是 ...