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的生命周期方法的更多相关文章

  1. Spring(十二):IOC容器中Bean的生命周期方法

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

  2. 一次性讲清楚spring中bean的生命周期之一:getSingleton方法

    要想讲清楚spring中bean的生命周期,真的是不容易,以AnnotationConfigApplicationContext上下文为基础来讲解bean的生命周期,AnnotationConfigA ...

  3. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  4. Spring中Bean的生命周期及其扩展点

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6106456.html Spring中Bean的管理是其最基本的功能,根据下面的图来了解Spr ...

  5. 简:Spring中Bean的生命周期及代码示例

    (重要:spring bean的生命周期. spring的bean周期,装配.看过spring 源码吗?(把容器启动过程说了一遍,xml解析,bean装载,bean缓存等)) 完整的生命周期概述(牢记 ...

  6. 通过BeanPostProcessor理解Spring中Bean的生命周期

    通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...

  7. 一分钟掌握Spring中bean的生命周期!

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean 的别名只能维持 ...

  8. Spring中bean的生命周期!

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  9. Spring中 bean的生命周期

    为什么要了解Spring中 bean的生命周期? 有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要. 二话不说先上图: 在谈具体流程之前先看看Spring官方 ...

随机推荐

  1. int(3)和int(10)的区别

    int(M) 在 integer 数据类型中,M 表示最大显示宽度.在 int(M) 中,M 的值跟 int(M) 所占多少存储空间并无任何关系. int(3).int(4).int(8) 在磁盘上都 ...

  2. 遇到 java.io.EOFException 异常的解决办法

    可以试着clean项目后再启动!原因未明

  3. 获取$(this)子节点对象的方法

    获取$(this)子节点对象的方法: 1.children()方法: children() 方法返回被选元素的所有直接子元素. 该方法只会向下一级对 DOM 树进行遍历. 2.find()方法: fi ...

  4. 常见的JavaScript函数

    JavaScript函数一共可分为5类:常规函数.数组函数.日期函数.数学函数和字符串函数. (1)常规函数(9个) alert函数:显示一个警告对话框,包括一个“确定”按钮. confirm函数:显 ...

  5. 原生Android App项目调用Untiy导出的Android项目

    背景:采用Google VR SDK for Unity 开发3D场景功能,然后导出Android项目,合并到一个Android App里面,供其它Activity调用. 用Google VR for ...

  6. B-树,B+树,B*树详解

    B-树 B-树是一种多路搜索树(并不一定是二叉的) 1970年,R.Bayer和E.mccreight提出了一种适用于外查找的树,它是一种平衡的多叉树,称为B树(或B-树.B_树). 一棵m阶B树(b ...

  7. jquery实现on/off开关按钮

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  8. WAP站点(IIS/Apache)的服务器设置

    Server 端的设置IIS服务器:为了使IIS支持WAP(WML)页面的发布,在IIS的Web站点的属性 / HTTP信息中设置WAP的MIME属性,添加如下的MIME类型:扩展名 内容类型(MIM ...

  9. 《JAVA与模式》之单例模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述单例模式的: 作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 单例模式的 ...

  10. <c:forEach>循环list,一个表格两列数据

    参考: http://zhidao.baidu.com/link?url=apG5dUmW7RjB5eOYKSWOWdKd7nxFpkDO4n3i8R6MWYKl7E2JC1OCtPILF4G4EUO ...