Spring生命周期各种接口使用
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生命周期各种接口使用的更多相关文章
- spring生命周期
Github地址 最近在整合mybatis-spring. 公司里面已经有一个叫做kylin-datasource的开发包,以前能够提供master和slave2个数据源,最近更新了2.0版本,支持自 ...
- 说下spring生命周期
面试官:说下spring生命周期 程序员:不会 那你先回去等消息吧 Bean实现了BeanNameAware,Spring会将Bean的ID透传给setBeanName java.后端开发.程 ...
- spring 生命周期最详解
转载. https://blog.csdn.net/qq_23473123/article/details/76610052 目的 在大三开始学习spring时,老师就说spring bean周期非常 ...
- Spring学习总结(4)-Spring生命周期的回调
参考文档:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans ...
- 【源码】spring生命周期
一.spring生命周期 1. 实例化Bean 对于BeanFactory容器,当客户向容器请求一个尚未初始化的bean时,或初始化bean的时候需要注入另一个尚未初始化的依赖时,容器就会调用crea ...
- Spring生命周期详解
导读 Spring中Bean的生命周期从容器的启动到停止,涉及到的源码主要是在org.springframework.context.support.AbstractApplicationContex ...
- Spring生命周期 Constructor > @PostConstruct > InitializingBean > init-method
项目中用到了 afterPropertiesSet: 于是具体的查了一下到底afterPropertiesSet到底是什么时候执行的.为什么一定要实现 InitializingBean; **/ @C ...
- spring生命周期流程图
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- 七、spring生命周期之初始化和销毁方法
一.通过@Bean指定初始化和销毁方法 在以往的xml中,我们是这样配置的 <bean id="exampleInitBean" class="examples.E ...
随机推荐
- 创建mysql数据库的时候指定编码
CREATE DATABASE xxx DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
- 计算机学院大学生程序设计竞赛(2015’12) 1004 Happy Value
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include ...
- bootstrap中的居左和居右
1.pull-left和pull-right 2.text-left.text-center和text-right
- php中print_r 和var_dump 打印变量的区别。
<?php $arr = array(true); var_dump($arr); echo "<br/>"; print_r($arr); 结果如下: 说明 p ...
- CentOS6.6部署OpenStack Havana(Nova-Network版)
CentOS6.4部署OpenStack Havana(Nova-Network版) 一 基本设备介绍 测试环境 CentOS6.4 x64 OpenStack 服务 介绍 计算 (Compute) ...
- CentOS 5.8 x64 源码安装 samba-3.6.9
环境 CentOS 5.8 X64 wget http://www.samba.org/samba/ftp/stable/samba-3.6.9.tar.gz tar zxvf samb ...
- Linux/hp unix/AIX日常巡检脚本(转)
以下为Linux/hp unix/AIX日常巡检脚本,大家可以参考着进行改写,用于自己的服务器. #!/usr/bin/ksh syserrdate=`date +"%m/%d"` ...
- FZU 2098 刻苦的小芳
这个问题转化一下就是求长度为2*n的正确括号匹配串,两个匹配的括号之间的距离不超过2*k的有几种. 假设左括号为1,右括号为-1,dp[i][j]表示长度为i的括号匹配串,前缀和为j的有几种.dp[2 ...
- 理解javascript函数的重载
javascript其实是不支持重载的,这篇文章会和java语言函数的重载对比来加深对javascript函数重载的理解. 以下我会假设读者不了解什么是重载所以会有一些很基础的概念 ...
- 浅谈IOS8之size class 分类: ios技术 2015-02-05 19:06 62人阅读 评论(0) 收藏
文章目录 1. 简介 2. 实验 3. 实战 3.1. 修改 Constraints 3.2. 安装和卸载 Constraints 3.3. 安装和卸载 View 3.4. 其他 4. 后话 以前和安 ...