Spring中Bean的生命周期方法
Bean的生命周期方法
src\dayday\Car.java
package dayday; import com.sun.org.apache.xpath.internal.SourceTree; import javax.sound.midi.Soundbank; /**
* Created by I am master on 2016/11/28.
*/
public class Car {
private String name;
public void setName(String name){
this.name=name;
System.out.println("setName...");
}
public String getName(){
return name;
}
public String toString(){
return "["+"carname="+name+"]";
}
public Car(){
System.out.println("Car's constructor...");
}
public void init(){
System.out.println("init...");
}
public void destroy(){
System.out.println("destroy...");
}
}
src\dayday\Main.java
package dayday; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.net.SocketTimeoutException;
import java.sql.SQLException; /**
* Created by I am master on 2016/11/28.
*/
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
Car car=ctx.getBean("car",Car.class);
System.out.println(car);
//用close()方法是就会调用bean对应的destroy()方法
ctx.close();
}
}
src\beans.xml
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="car" class="dayday.Car" init-method="init" destroy-method="destroy">
<property name="name" value="Audi"/>
</bean>
</beans> 运行结果
Car's constructor...
setName...
init...
[carname=Audi]
destroy...
bean的生命周期进行管理的过程分为五步
1)通过构造方法或工厂方法创建bean实例
2)为bean的属性设置值和对其他bean的引用
3)调用bean的初始化方法
4)使用bean
5)当spring容器关闭时,调用bean的销毁方法
bean后置处理器
在调用初始化工作前后对bean进行额外的操作
需要实现BeanPostProcessor接口
postProcessBeforeInitialization(Object bean, String beanName)在调用init()方法之前调用
postProcessAfterInitialization(Object bean, String beanName)在调用init()方法之后调用
src\dayday\MyBeanPostProcessor.java
package dayday; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; /**
* Created by I am master on 2016/11/29.
*/
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization "+bean+" "+beanName);
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization "+bean+" "+beanName);
return bean;
}
}
Main/Car.java代码不发生变化
src\bean.xml
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="car" class="dayday.Car" init-method="init" destroy-method="destroy">
<property name="name" value="Audi"/>
</bean>
<!--配置bean的后置处理器-->
<bean class="dayday.MyBeanPostProcessor"></bean>
</beans>
运行结果:
Car's constructor...
setName...
postProcessBeforeInitialization [carname=Audi] car
init...
postProcessAfterInitialization [carname=Audi] car
[carname=Audi]
destroy...
//调用close()方式是就会调用bean中的destroy()方法
Spring中Bean的生命周期方法的更多相关文章
- Spring(十二):IOC容器中Bean的生命周期方法
IOC容器中Bean的生命周期方法 1)Spring IOC容器可以管理Bean的声明周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2)Spring IOC容器对Bean的生命周期 ...
- 一次性讲清楚spring中bean的生命周期之一:getSingleton方法
要想讲清楚spring中bean的生命周期,真的是不容易,以AnnotationConfigApplicationContext上下文为基础来讲解bean的生命周期,AnnotationConfigA ...
- JAVA面试题:Spring中bean的生命周期
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...
- Spring中Bean的生命周期及其扩展点
原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...
- 简:Spring中Bean的生命周期及代码示例
(重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...
- 通过BeanPostProcessor理解Spring中Bean的生命周期
通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...
- 一分钟掌握Spring中bean的生命周期!
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean 的别名只能维持 ...
- Spring中bean的生命周期!
Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...
- Spring中 bean的生命周期
为什么要了解Spring中 bean的生命周期? 有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要. 二话不说先上图: 在谈具体流程之前先看看Spring官方 ...
随机推荐
- .net 导出Excel功能
将DataSet对象导出成Excel文档 一.不带格式控制 void btnExport_Click(object sender, EventArgs e) { IList<string> ...
- 弹层组件-layer
layer是Layui的一个弹层组建,功能强大,总之我很喜欢,下面介绍这个组件的基本用法. 首先如果只需要使用layer而不想使用Layui可以单独下载layer组件包,页面引入jquery1.8以上 ...
- ExtJS获取父子、兄弟容器元素方法
http://www.cnblogs.com/CoolHu/archive/2012/12/08/2808433.html 1.当前对象的父对象(上级对象) this.ownerCt: 2.当前对象的 ...
- Load Runner录制C/S客户端
1. 打开应用程序 2. 点击如下菜单 弹出窗口如下 3. 点击New,弹出窗口如下,选择Web(HTTP/HTML) 4. 点击Create,弹出窗口 5. 点击OK, ...
- Python 绘制图表之我见 ---一个java程序员的看法
---------------- 环境: win 10 . python3.5 https://github.com/Leechen2014/1400OS_01_Codes/blob/master/ ...
- 缺少wlanapi.dll文件问题修复
我在下载百度云盘的时候碰到了一个问题,缺少wlanapi.dll文件.下面贴出解决办法 第一步 http://d.apktop.cn/p/soft_134.html 下载wlanapi.dll 第 ...
- Ubuntu: ImportError: No module named xgboost
ImportError: No module named xgboost 解决办法: git clone --recursive https://github.com/dmlc/xgboost cd ...
- C#webform LinQ
LinQ的高级查询: 模糊查 con.Car.Where(r=>r.Name.Contains(cname)).ToList(); 以..开头 con.car.Where(r => r.n ...
- explain 执行计划详解
id:id是一组数字,表示查询中执行select子句或操作表的顺序,如果id相同,则执行顺序从上至下,如果是子查询,id的序号会递增,id越大则优先级越高,越先会被执行. id列为null的就表是这是 ...
- Android App的签名打包三步骤
1.签名的意义 为了保证每个应用程序开发商合法ID,防止部分开放商可能通过使用相同的Package Name来混淆替换已经安装的程序,我们需要对我们发布的APK文件进行唯一签名,保证我们每次发布的版本 ...