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官方 ...
随机推荐
- 我是如何反编译D-Link路由器固件程序并发现它的后门的
OK,又是周末晚上,没有约会,只有一大瓶Shasta汽水和全是快节奏的音乐…那就研究一下程序吧. 一时兴起,我下载了D-link无线路由器(型号:DIR-100 revA)的固件程序 v1.13.使用 ...
- 20155229-付钰涵-分析自我技能延展到c语言学习状况
我的小技能 我记得幼儿园时表演的舞蹈,也记得从水彩到素描的学习,还记得小学和初中获得的钢琴省级奖项. 舞蹈止于一年级,绘画止于三年级,钢琴从学前班到高一那十年的时间里有过断续. 03年-04年的那个冬 ...
- 卸载mysql
如果你的电脑里装过MySQL,想再重新安装MySQL的时候可能就会因为前一版本卸载不彻底而出现错误.最常见的就是安装好后设置参数的最后一步验证时,会在Execute configurattion步骤中 ...
- Linux下配置Node.js环境
1.下载代码 下载地址:https://nodejs.org/en/download/ 下载Linux Binaries (.tar.xz)版本,位数根据操作系统位数选择 2.复制代码包 用FTP上传 ...
- Centos下MySQL主从同步配置
说明:由于MySQL不同版本之间的(二进制日志)binlog格式可能会不一样, 因此最好的搭配组合是Master的MySQL版本和Slave的版本相同或者更低,Master的版本肯定不能高于Slave ...
- Python:dict用法
dict全称dictionary,使用键-值(key-value)存储,有极快的查找速度. 以下整理几种常用的dict用法 定义 空dict >>> dict={} 普通dict & ...
- Mongodb 副本集分片(一)---初始化mongodb安装启动
写在前面:mongodb是nosql非关系型数据库中,比较受欢迎的产品.在数据持久化及与关系型数据库的关联上也做的比较好,目前各大公司在存放二进制文件(图片.视频等)中应用也比较广泛.其遵循的key- ...
- [原创]C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。
首先介绍基本WindowsApi: public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 函 ...
- 【思路】-分页-双top分页算法的原理
描述:实现分页的一种算法 大致过程:访客访问不同的分页,为这个当前页生成动态的查询SQL,然后送到数据库中执行 输入:总条数,每页多少条,第几页,查询的SQL,排序的字段 注意:传入的排序字段需要构成 ...
- 实验7 BindService模拟通信
实验报告 课程名称 基于Android平台移动互联网开发 实验日期 2016.4.16 实验项目名称 BindService模拟通信 实验地点 S30010 实验类型 □验证型 √设计型 ...