Spring bean的生命周期

来源:互联网 发布:贝爷生存刀淘宝网 编辑:程序博客网 时间:2024/06/05 06:17
LifeCycleBean.java
package beanliftcycle;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.beans.factory.config.BeanPostProcessor;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;@Componentpublic class LiftCycleBean implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,BeanPostProcessor,InitializingBean,DisposableBean{private void info(String infoMsg){System.out.println(infoMsg);}@Overridepublic void setBeanName(String arg0) {info("步骤三:如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBeanName方法");}@Overridepublic void setBeanFactory(BeanFactory arg0) throws BeansException {info("步骤四:如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory方法,将BeanFactoryAware方法,将BeanFactory容器实例传入");}@Overridepublic void setApplicationContext(ApplicationContext arg0) throws BeansException {info("步骤五:如果bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext方法,将bean所在的应用上下文引用传入进来");}@Overridepublic Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {info("步骤七:如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization方法arg0="+arg0+"arg1"+arg1);return arg0;}@Overridepublic void afterPropertiesSet() throws Exception {info("步骤六:如果bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet方法。类似地,如果bean使用init-method声明了初始化方法,该方法也会被调用");}@Overridepublic Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {info("步骤八:如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization方法arg0="+arg0+"arg1"+arg1);return arg0;}@Overridepublic void destroy() throws Exception {info("步骤十:如果bean实现了DisposableBeanjiekou ,Spring将调用它的destroy接口方法。同样,如果bean使用destroy-method声明了销毁方法,该方法也会被调用");}}


JavaConfig.java
package beanliftcycle;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScanpublic class JavaConfig {}

M.java
jpackage beanliftcycle;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class M {public static void main(String[] args) {AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(JavaConfig.class);for (String s : annotationConfigApplicationContext.getBeanDefinitionNames()) {System.out.println("实例化了 "+s.substring(s.lastIndexOf('.')+1));}annotationConfigApplicationContext.close();}}

输出
十二月 18, 2017 8:48:58 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2e817b38: startup date [Mon Dec 18 20:48:58 CST 2017]; root of context hierarchy步骤三:如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBeanName方法步骤四:如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory方法,将BeanFactoryAware方法,将BeanFactory容器实例传入步骤五:如果bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext方法,将bean所在的应用上下文引用传入进来步骤六:如果bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet方法。类似地,如果bean使用init-method声明了初始化方法,该方法也会被调用步骤七:如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization方法arg0=org.springframework.context.event.EventListenerMethodProcessor@545997b1arg1org.springframework.context.event.internalEventListenerProcessor步骤八:如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization方法arg0=org.springframework.context.event.EventListenerMethodProcessor@545997b1arg1org.springframework.context.event.internalEventListenerProcessor步骤七:如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization方法arg0=org.springframework.context.event.DefaultEventListenerFactory@77846d2carg1org.springframework.context.event.internalEventListenerFactory步骤八:如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization方法arg0=org.springframework.context.event.DefaultEventListenerFactory@77846d2carg1org.springframework.context.event.internalEventListenerFactory步骤七:如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization方法arg0=beanliftcycle.JavaConfig$$EnhancerBySpringCGLIB$$d240f1a0@548ad73barg1javaConfig步骤八:如果bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization方法arg0=beanliftcycle.JavaConfig$$EnhancerBySpringCGLIB$$d240f1a0@548ad73barg1javaConfig实例化了 internalConfigurationAnnotationProcessor实例化了 internalAutowiredAnnotationProcessor实例化了 internalRequiredAnnotationProcessor实例化了 internalCommonAnnotationProcessor实例化了 internalEventListenerProcessor实例化了 internalEventListenerFactory实例化了 javaConfig实例化了 liftCycleBean十二月 18, 2017 8:48:58 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2e817b38: startup date [Mon Dec 18 20:48:58 CST 2017]; root of context hierarchy步骤十:如果bean实现了DisposableBeanjiekou ,Spring将调用它的destroy接口方法。同样,如果bean使用destroy-method声明了销毁方法,该方法也会被调用



原创粉丝点击