您好,登錄后才能下訂單哦!
這篇文章主要講解了“Spring bean生命周期知識點講解”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Spring bean生命周期知識點講解”吧!
前言
任何一個事物都有自己的生命周期,生命的開始、生命中、生命結束。大家最熟悉的應該是servlet 的生命周期吧。和 servlet 一樣 spring bean 也有自己的生命周期。
在開發中生命周期是一個很常見的名詞,基本每種編程語言都能找到與它關聯的。關于bean的生命周期我在網上也找了好多,基本都差不多。這里我主要是想通過代碼來驗證,畢竟學的知識都是一樣的,都是學的Java,最重要的是動手練習,這樣印象更深。
下面是它生命周期的描述,我們通過demo來進行驗證。
下圖是它執行的順序。
一、創建LiftCycle類實現BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware5個接口方法
package Cuiyw.Spring.Service; 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; public class LifeCycle implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware{ private String name; public String getName() { System.out.println("getName name="+name); return name; } public void setName(String name) { System.out.println("setName name="+name); this.name = name; } public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("InitializingBean.afterPropertiesSet()"); } public void setBeanName(String arg0) { // TODO Auto-generated method stub System.out.println("BeanNameAware.setBeanName"); } public void setBeanFactory(BeanFactory arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanFactoryAware.setBeanFactory"); } public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("DisposableBean.destroy"); } public void myInit() { System.out.println("【init-method】調用<bean>的init-method屬性指定的初始化方法"); } public void myDestory() { System.out.println("【destroy-method】調用<bean>的destroy-method屬性指定的初始化方法"); } public void setApplicationContext(ApplicationContext arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("ApplicationContextAware.setApplicationContext"); } }
二、注冊InstantiationAwareBeanPostProcessor接口
package Cuiyw.Spring.Service; import java.beans.PropertyDescriptor; import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor{ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInitialization"); return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization"); return bean; } public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation"); return true; } public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation"); return null; } public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues"); return pvs; } }
三、注冊BeanPostProcessor接口
其實InstantiationAwareBeanPostProcessor繼承BeanPostProcessor,所以在上面我也實現了BeanPostProcessor接口的方法
package Cuiyw.Spring.Service; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanPostProcessor.postProcessAfterInitialization "); return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanPostProcessor.postProcessBeforeInitialization"); return bean; } }
四、注冊BeanFactoryPostProcessor接口
package Cuiyw.Spring.Service; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanFactoryPostProcessor.postProcessBeanFactory"); } }
五、在上下文中配置
這里還是在上一個博客demo的基礎上進行修改,為了有其他干擾,我先把service去掉了。
<?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 id="beanPostProcessor" class="Cuiyw.Spring.Service.MyBeanPostProcessor"></bean> <bean id="instantiationAwareBeanPostProcessor" class="Cuiyw.Spring.Service.MyInstantiationAwareBeanPostProcessor"></bean> <bean id="beanFactoryPostProcessor" class="Cuiyw.Spring.Service.MyBeanFactoryPostProcessor"></bean> <bean id="lifeCycle" class="Cuiyw.Spring.Service.LifeCycle" init-method="myInit" destroy-method="myDestory"> <property name="name" value="cuiyw1"></property> </bean> </beans>
六、在main中使用bean
package Cuiyw.SpringAop; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import Cuiyw.Spring.IService.IService; import Cuiyw.Spring.Service.LifeCycle; public class App { public static void main( String[] args ) { ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"}); BeanFactory factory=context; LifeCycle lifeCycle=factory.getBean("lifeCycle",LifeCycle.class); lifeCycle.setName("cuiyw2"); System.out.println("lifeCycle.name="+lifeCycle.getName()); ((ClassPathXmlApplicationContext)factory).registerShutdownHook(); /*service=(IService)factory.getBean("ServiceA"); service.service("Cuiyw ServiceA"); service=(IService)factory.getBean("ServiceImpl"); service.service("Cuiyw ServiceImpl"); */ } }
七、輸入打印結果
可以發現輸出的順序和上面圖的順序基本一致。
感謝各位的閱讀,以上就是“Spring bean生命周期知識點講解”的內容了,經過本文的學習后,相信大家對Spring bean生命周期知識點講解這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。