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. Libpci库的调用

    这几天发现在Redhat AS6.5 X86_64下用outl(index, 0xcf8)和inl(0xcfc)下读取PCIe配置空间是系统有时性的会hang, 于是去寻找解决方案,首先想到的是用/d ...

  2. text/plain && text/html

    text/plain和text/html都是Content-Type; text/plain : 页面以文本形式输出 text/html:  页面以html格式输出

  3. ES6 标准部分应用

    1.多行字符串 字符串换行时,不再使用\n,而是使用倒引号`..`,例如: alert(`这是一个 多行 字符串`); 2.模版字符串 不再使用"+"来拼接字符串与变量,而是使用倒 ...

  4. Html锚点定位偏差计算解决插件

    /*=============== 以下为HTML中的锚点代码 =====================*/ <div id="fixedNavBar" class=&qu ...

  5. 基础算法之插入排序Insertion Sort

    原理 通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元 ...

  6. PHP之OOP要点摘要

    类和对象:    类是生成对象的模板,对象是活动组件;    面向对象编程实际操作都是通过类的实例(而不是类本身)完成的:    访问控制(public.protected.private):(1) ...

  7. Dns 类

    Dns 类型公开以下成员. 方法       名称 说明 BeginGetHostAddresses 异步返回指定主机的 Internet 协议 (IP) 地址. BeginGetHostByName ...

  8. ThinkPHP 3.2.3(三)架构之URL模式

    一.标准URL格式 http://serverName/index.php/模块/控制器/操作   二.URL大小写 在/ThinkPHP/Conf/convention.php文件里有URL大小写的 ...

  9. GitHub的使用之新建与更新代码

    一.新建仓储:注册GitHub后你就会有0.3G的免费空间,不过只能创建公开项目,这也满足代码分享的目的,我最喜欢的倒是它的代码展示方式,可以直接浏览你的代码,代码是经过高亮.添加行号处理过的,十分漂 ...

  10. Spring整合HBase

    Spring整合HBase Spring HBase SHDP § 系统环境 § 配置HBase运行环境 § 配置Hadoop § 配置HBase § 启动Hadoop和HBase § 创建Maven ...