1,BeanPostProcessor接口;
不能在POJO上面使用,需要单独找一个类进行使用;
如果在POJO上面实现了此接口,在实现了其他*Aware
接口之后,这个接口方法将不会被调用;
2, POJO实现了BeanNameAware接口;
可以与BeanPostProcessor同时使用;
3, POJO实现了BeanFactoryAware接口;
可以与BeanPostProcessor同时使用;
4, POJO实现了ApplicationContextAware接口;
可以与BeanPostProcessor同时使用;
5,POJO实现了DisposableBean接口;
可以正常调用接口的dispose()方法;
6,在bean声明部分定义的init-method
和destroy-method方法可以正常调用;
7,如果显示调用dispose()、destroy-method方法,
需要使用
AbstractApplicationContext context =
new ClassPathXmlApplicationContext("appbeans.xml");
//...
context.registerShutdownHook();

8,InitializingBean.afterPropertiesSet()方法在init-method()之前调用;

DisposableBean.dispose()方法在destroy-method()之前调用;

声明周期图:

http://my.oschina.net/u/218421/blog/37743

package com.stono.sprtest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Minstrel implements BeanNameAware, BeanFactoryAware,
ApplicationContextAware, InitializingBean, DisposableBean { public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(
"appbeans.xml");
Minstrel minstrel = (Minstrel) context.getBean("minstrel");
minstrel.show();
context.registerShutdownHook();
} private String accordion;
private Logger logger = Logger.getLogger(Minstrel.class); private String song; public Minstrel() {
logger.info("default constructor");
} public Minstrel(String song) {
logger.info("song constructor");
this.song = song;
} public Minstrel(String song, String accordion) {
logger.info("song accordion constructor");
this.song = song;
this.accordion = accordion;
} public void destroyMethod() {
logger.info("destroyMethod called");
} public String getAccordion() {
logger.info("getAccordion method");
return accordion;
} public String getSong() {
logger.info("getSong method");
return song;
} public void initMethod() {
logger.info("initMethod called");
} public void setAccordion(String accordion) {
logger.info("setAccordion method");
this.accordion = accordion;
} public void setSong(String song) {
logger.info("setSong method");
this.song = song;
} public void show() {
System.out.println("show method called");
} @Override
public void setBeanName(String name) {
logger.info("com.stono.sprtest.Minstrel.setBeanName(String):" + name);
} @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
logger.info("com.stono.sprtest.Minstrel.setBeanFactory(BeanFactory):"
+ beanFactory);
} @Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
logger.info("com.stono.sprtest.Minstrel.setApplicationContext(ApplicationContext):"
+ applicationContext);
} @Override
public void afterPropertiesSet() throws Exception {
logger.info("com.stono.sprtest.Minstrel.afterPropertiesSet()");
} @Override
public void destroy() throws Exception {
logger.info("com.stono.sprtest.Minstrel.destroy()");
} }
package com.stono.sprtest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; public class AppBeanPostProcessor implements BeanPostProcessor { @Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessBeforeInitialization is called");
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessAfterInitialization is called");
return bean;
} }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="minstrel" class="com.stono.sprtest.Minstrel"
init-method="initMethod" destroy-method="destroyMethod">
<constructor-arg name="song" value="SongSong"></constructor-arg>
<property name="accordion">
<value>oldAccordion</value>
</property>
</bean>
<bean class="com.stono.sprtest.AppBeanPostProcessor"></bean>
</beans>

程序输出:

2015-9-18 7:55:26 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@89fbe3: startup date [Fri Sep 18 07:55:26 CST 2015]; root of context hierarchy
2015-9-18 7:55:26 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [appbeans.xml]
song constructor
setAccordion method
com.stono.sprtest.Minstrel.setBeanName(String):minstrel
com.stono.sprtest.Minstrel.setBeanFactory(BeanFactory):org.springframework.beans.factory.support.DefaultListableBeanFactory@cbf30e: defining beans [minstrel,com.stono.sprtest.AppBeanPostProcessor#0]; root of factory hierarchy
com.stono.sprtest.Minstrel.setApplicationContext(ApplicationContext):org.springframework.context.support.ClassPathXmlApplicationContext@89fbe3: startup date [Fri Sep 18 07:55:26 CST 2015]; root of context hierarchy
postProcessBeforeInitialization is called
com.stono.sprtest.Minstrel.afterPropertiesSet()
initMethod called
postProcessAfterInitialization is called
show method called
2015-9-18 7:55:26 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@89fbe3: startup date [Fri Sep 18 07:55:26 CST 2015]; root of context hierarchy
com.stono.sprtest.Minstrel.destroy()
destroyMethod called

Spring生命周期各种接口使用的更多相关文章

  1. spring生命周期

    Github地址 最近在整合mybatis-spring. 公司里面已经有一个叫做kylin-datasource的开发包,以前能够提供master和slave2个数据源,最近更新了2.0版本,支持自 ...

  2. 说下spring生命周期

    面试官:说下spring生命周期 程序员:不会 那你先回去等消息吧     Bean实现了BeanNameAware,Spring会将Bean的ID透传给setBeanName java.后端开发.程 ...

  3. spring 生命周期最详解

    转载. https://blog.csdn.net/qq_23473123/article/details/76610052 目的 在大三开始学习spring时,老师就说spring bean周期非常 ...

  4. Spring学习总结(4)-Spring生命周期的回调

    参考文档:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans ...

  5. 【源码】spring生命周期

    一.spring生命周期 1. 实例化Bean 对于BeanFactory容器,当客户向容器请求一个尚未初始化的bean时,或初始化bean的时候需要注入另一个尚未初始化的依赖时,容器就会调用crea ...

  6. Spring生命周期详解

    导读 Spring中Bean的生命周期从容器的启动到停止,涉及到的源码主要是在org.springframework.context.support.AbstractApplicationContex ...

  7. Spring生命周期 Constructor > @PostConstruct > InitializingBean > init-method

    项目中用到了 afterPropertiesSet: 于是具体的查了一下到底afterPropertiesSet到底是什么时候执行的.为什么一定要实现 InitializingBean; **/ @C ...

  8. spring生命周期流程图

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  9. 七、spring生命周期之初始化和销毁方法

    一.通过@Bean指定初始化和销毁方法 在以往的xml中,我们是这样配置的 <bean id="exampleInitBean" class="examples.E ...

随机推荐

  1. c语言学习基础:[1]开发工具介绍

    标签:c语言 1 2 3 4 分步阅读 学习编程语言的童鞋们一开始接触到的最多的估计就是C语言了,其次才是什么java.c++等,可以说学习c语言是我们走向编程世界的一座桥梁,学好它,对于我们学习和研 ...

  2. Delphi XE7 Update1 下载破解、带源码和帮助安装序列号

    源:http://blog.csdn.net/tht2009/article/details/39157877 Delphi安装与破解 1.XE7 XE7Update1:http://altd.emb ...

  3. Firebug 非常好用

    chrome 浏览器 火狐 直接获取网页中传输的数据

  4. [Unity UGUI]UGUI提供多种不同的解决方案

    现代游戏和应用程序经常需要支持各种不同的屏幕分辨率,特别是UI布局需要能够适应.UI系统在统一中包括各种工具来实现此目的,可以以多种方式组合在一起. 在这个小节我们将使用一个简单的案例研究和观察和比较 ...

  5. MySQL的char和varchar

    一.VARCHAR与CHAR字符型数据的差异 在MySQL数据库中,用的最多的字符型数据类型就是Varchar和Char,这两种数据类型虽然都是用来存放字符型数据,但是无论从结构还是从数据的保存方式来 ...

  6. STM32 对内部FLASH读写接口函数(转)

    源:STM32 对内部FLASH读写接口函数 因为要用内部FLASH代替外部EEPROM,把参数放在STM32的0x08000000+320K处,其中20K是bootloader,300K是应用程序. ...

  7. iOS 页面跳转,离开之前pop掉navigationController栈中的页面

    http://blog.csdn.net/worldzhy/article/details/41312713 - (void)viewDidDisappear:(BOOL)animated { //因 ...

  8. 理解javascript函数的重载

        javascript其实是不支持重载的,这篇文章会和java语言函数的重载对比来加深对javascript函数重载的理解.       以下我会假设读者不了解什么是重载所以会有一些很基础的概念 ...

  9. LPC1768IAP(详解,有上位机)

    之前说了stm32的iap编程,今天天气真好,顺手就来说说lpc1788的iap编程(没看前面的请查看stm笔记下的内容) 首先是flash的算法,lpc1768并没有寄存器来让我们操作flash,他 ...

  10. BLE 安卓APP控制LED灯的实现(转)

    源:BLE 安卓APP控制LED灯的实现 //注:参考AmoMcu源代码修改. 打开APP,检查蓝牙是否打开 BluetoothAdapter mBluetoothAdapter; final Blu ...