Spring Bean 生命周期示意图:
了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程。
---------------------------------------------------------------------------------------------------------------------------------------------------
spring-service.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 定义一个bean --> <bean id="narCodeService" class="com.test.service.impl.NarCodeServiceImpl"> </bean> <bean id="beanLifecycle" class="com.test.spring.BeanLifecycle" init-method="init"> <property name="name" value="张三"></property> <property name="sex" value="男"></property> </bean> </beans>
Service Class:
package com.test.spring; 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.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.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.EnvironmentAware; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.ImportAware; import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; import com.sun.org.apache.xml.internal.security.Init; /** * 测试Spring Bean的生命周期 * @author zss * */ public class BeanLifecycle implements InitializingBean,DisposableBean ,BeanFactoryAware,BeanNameAware,BeanPostProcessor{ private String name; private String sex; public String getName() { return name; } public void setName(String name) { System.out.println("》》》调用BeanLifecycle对象"+this.getName()+"属性set方法,设值为:"+name); this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { System.out.println("》》》调用BeanLifecycle对象"+this.getSex()+"属性set方法,设值为:"+sex); this.sex = sex; } public void init (){ System.out.println("》》》init方法被调用"); } public void close() { System.out.println("》》》close方法被调用"); } @Override public void afterPropertiesSet() throws Exception { System.out.println(this); System.out.println("》》》BeanLifecycle调用了InitailizingBean的afterPorpertiesSet方法了....."); } @Override public void destroy() throws Exception { System.out.println("》》》BeanLifecycle从Spring IoC容器中移除了......."); } // @Override // public void setApplicationContext(ApplicationContext paramApplicationContext) // throws BeansException { // System.out.print("》》》调用ApplicationContextAware接口setApplicationContext方法:"); // System.out.println(paramApplicationContext); // // } // @Override // public void setResourceLoader(ResourceLoader paramResourceLoader) { // System.out.print("》》》调用ResourceLoaderAware接口setResourceLoader方法:"); // System.out.println(paramResourceLoader); // } // @Override // public void setImportMetadata(AnnotationMetadata paramAnnotationMetadata) { // System.out.println(333333); // } // @Override // public void setEnvironment(Environment paramEnvironment) { // System.out.print("》》》调用EnvironmentAware接口setEnvironment方法:"); // System.out.println(paramEnvironment); // } @Override public void setBeanName(String paramString) { System.out.println("》》》调用BeanNameAware接口setBenaName方法: "+paramString); } @Override public void setBeanFactory(BeanFactory paramBeanFactory) throws BeansException { System.out.print("》》》调用BeanFactoryAware接口setBeanFactory方法:"); System.out.println(paramBeanFactory); } // @Override // public void setBeanClassLoader(ClassLoader paramClassLoader) { // System.out.print("》》》调用BeanClassLoaderAware接口setBeanClassLoader方法:"); // System.out.println(paramClassLoader); // } // @Override // public void setApplicationEventPublisher( // ApplicationEventPublisher paramApplicationEventPublisher) { // System.out.print("》》》调用ApplicationEventPublisherAware接口setApplicationEventPublisher方法:"); // System.out.println(paramApplicationEventPublisher); // } @Override public String toString() { return "BeanLifecycle [name=" + name + ", sex=" + sex + "]"; } @Override public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { System.out.println("调用了postProcessAfterInitialization方法"); return null; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { System.out.println("调用了postProcessBeforeInitialization方法"); return null; } }
Test:
package com.test.spring; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class T { ClassPathXmlApplicationContext applicationcontext=null; @Before public void before() { System.out.println("》》》Spring ApplicationContext容器开始初始化了......"); applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"}); System.out.println("》》》Spring ApplicationContext容器初始化完毕了......"); } @Test public void test() { BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class); //applicationcontext.close(); applicationcontext.registerShutdownHook(); } }
测试结果:
》》》Spring ApplicationContext容器开始初始化了...... 2017-03-18 20:29:20 INFO:ClassPathXmlApplicationContext-Refreshing org[email protected]6c7e6b26: startup date [Sat Mar 18 20:29:20 CST 2017]; root of context hierarchy 2017-03-18 20:29:20 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml] 》》》调用BeanLifecycle对象null属性set方法,设值为:周四上 》》》调用BeanLifecycle对象null属性set方法,设值为:男 》》》调用BeanNameAware接口setBenaName方法: beanLifecycle 》》》调用BeanFactoryAware接口setBeanFactory方法:org.s[email protected]2b763bac: defining beans [narCodeService,beanLifecycle]; root of factory hierarchy BeanLifecycle [name=张三, sex=男] 》》》BeanLifecycle调用了InitailizingBean的afterPorpertiesSet方法了..... 》》》init方法被调用 调用了postProcessBeforeInitialization方法 》》》Spring ApplicationContext容器初始化完毕了...... 2017-03-18 20:29:20 INFO:ClassPathXmlApplicationContext-Closing org[email protected]6c7e6b26: startup date [Sat Mar 18 20:29:20 CST 2017]; root of context hierarchy 》》》BeanLifecycle从Spring IoC容器中移除了....... 》》》close方法被调用
时间: 2024-12-12 09:16:36