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***"); } }
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="springBean" class="com.lxh.springBeanLifeCycle.SpringBean" singleton="true" init-method="initMethod" destroy-method="destroyMethod"> <property name="name" value="lxh" /> </bean> </beans>
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(); } }
運行結果: java