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 ...
随机推荐
- Commons Codec基本使用(转载)
在实际的应用中,我们经常需要对字符串进行编解码,Apache Commons家族中的Commons Codec就提供了一些公共的编解码实现,比如Base64, Hex, MD5,Phonetic an ...
- HTML知识点
1. 首页布局 // 顶部DIV#top{ position:absolute; top:; left:; width:100%; height:15%; overflow:hidden; margi ...
- unittest单元测试框架总结
unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果.今天笔者 ...
- ZOJ 3940 Modulo Query
0--M对某个数字取模,相当于把0--M区间进行切割,每次暴力切割一下.结果的算的时候二分一下即可... 看了官方题解才会... #include<cstdio> #include< ...
- NBUT 1457 Sona
莫队算法+离散化 1.map会TLE,必须离散化做 2.long long会WA,__int64定义 %I64d输出输出能AC 3.注意输入的序列会爆int #include<cstdio> ...
- c#之时间戳与DateTime的相互转换
1. 时间戳转 DateTime static DateTime GetServerNow(ulong serverTime) { DateTime dateTimeStart = TimeZone. ...
- [FTP]xferlog日志解析
[root@teacher ~]# cat /var/log/xferlogMon Jan 25 20:41:39 2016 1 10.0.222.156 913268 /sys/sys64/Pack ...
- Activiti----hellowWorld(使用H2数据库)
1.项目结构 2.pom <dependencies> <dependency> <groupId>junit</groupId> <artifa ...
- mysqlslap 使用总结
mysqlslap 可以用于模拟服务器的负载,并输出计时信息.其被包含在 MySQL 5.1 的发行包中.测试时,可以指定并发连接数,可以指定 SQL 语句.如果没有指定 SQL 语句,mysqlsl ...
- Mysql导入zabbix的sql语句时报错:ERROR 1045 (28000)
#Warning: Using a password on the command line interface can be insecure.#ERROR 1045 (28000): Access ...