package com.lxh.springBeanLifeCycle;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanClassLoaderAware;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.BeanNameAware;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.context.ApplicationEventPublisher;import org.springframework.context.ApplicationEventPublisherAware;import org.springframework.context.MessageSource;import org.springframework.context.MessageSourceAware;import org.springframework.context.ResourceLoaderAware;import org.springframework.core.io.ResourceLoader;public class SpringBean implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, ApplicationEventPublisherAware, MessageSourceAware, ApplicationContextAware, BeanPostProcessor, InitializingBean { // 属性 private String name; // set,get public String getName() { return name; } public void setName(String name) { this.name = name; } // spring bean 生命周期 @Override public void setBeanName(String arg0) { System.out.println("***BeanNameAware's setBeanName***\n" + arg0); } @Override public void setBeanClassLoader(ClassLoader arg0) { System.out.println("***BeanClassLoaderAware's setBeanClassLoader***\n" + arg0); } @Override public void setBeanFactory(BeanFactory arg0) throws BeansException { System.out.println("***BeanFactoryAware's setBeanFactory***\n" + arg0); } @Override public void setResourceLoader(ResourceLoader arg0) { System.out .println("***ResourceLoaderAware's setResourceLoader (only applicable when running in an application context)***\n" + arg0); } @Override public void setApplicationEventPublisher(ApplicationEventPublisher arg0) { System.out .println("***ApplicationEventPublisherAware's setApplicationEventPublisher (only applicable when running in an application context)***\n" + arg0); } @Override public void setMessageSource(MessageSource arg0) { System.out .println("***MessageSourceAware's setMessageSource (only applicable when running in an application context)***\n" + arg0); } @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { System.out .println("***ApplicationContextAware's setApplicationContext (only applicable when running in an application context)***\n" + arg0); } // 该方法实现ServletContextAware接口,只在web项目中使用,在ApplicationContextAware的setApplicationContext与BeanPostProcessors的postProcessBeforeInitialization之间 // @Override // public void setServletContext(ServletContext arg0) { // System.out.println("***ServletContextAware's setServletContext (only applicable when running in a web application context)***\n" // + arg0); // } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { System.out .println("***postProcessBeforeInitialization methods of BeanPostProcessors***\n" + arg0 + "\n" + arg1); return null; } @Override public void afterPropertiesSet() throws Exception { System.out.println("***InitializingBean's afterPropertiesSet***\n"); } // 自定义的初始化方法 public void initMethod() { System.out.println("***进入初始化方法****"); } @Override public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { System.out .println("***postProcessAfterInitialization methods of BeanPostProcessors***\n" + arg0 + "\n" + arg1); return null; } // 自定义的销毁方法 public void destroyMethod() { System.out.println("***bean's lifeCycle is over***"); }}
2. applicationContext.xml
3. Test.java-------测试类
package com.lxh.springBeanLifeCycle;import org.apache.log4j.Logger;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { // ApplicationContext public static ApplicationContext context = new ClassPathXmlApplicationContext( Test.class.getResource("").toString() + "/applicationContext.xml"); // 日志 @SuppressWarnings("unused") private static Logger logger = Logger.getLogger(Test.class); // main public static void main(String[] args) { SpringBean sb = (SpringBean) context.getBean("springBean"); System.out.println("***"+sb+"***"); // 销毁bean ((ClassPathXmlApplicationContext)context).close(); }}
运行结果: