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