IOC操作Bean管理XML方式(bean 的生命周期)
目錄
?
IOC操作Bean管理XML方式(bean 的生命周期)
?1.bean 的生命周期(在單例模式下的生命周期)
(1)通過構造器創建 bean 的實例(執行類中無參構造方法去創建對象)
(2)為 bean 的屬性設置值、或者對其他外部 bean 的引用(調用set 方法注入屬性)
(3)調用 bean 的初始化方法(需要我們進行配置初始化的方法)
(4)bean 可以使用了(也就是說對象獲取到了)
(5)當容器關閉的時候,調用 bean 的銷毀的方法(需要進行配置銷毀的方法)
2.演示bean? 的生命周期
步驟1:創建一個bean包,并且創建一個Orders類
步驟2:新建一個bean4.xml
步驟3:測試方法調用以及手寫銷毀方法調用
步驟4:實驗結果
?3.補充:bean 的后置處理器,bean的生命周期有七步
?4.演示添加后置處理器效果
(1)創建一個MyBeanPost 類,實現接口 BeanPostProcessor,創建后置處理器
(2)上面的MyBeanPost類實例出來,但是就是一個普通類,咱們Spring 并不知道這是一個后置處理器,所以我們需要進行配置
(3)分析測試結果
?
IOC操作Bean管理XML方式(bean 的生命周期)
?
什么是生命周期:從一個對象創建到一個對象銷毀的過程就是生命周期
bean 的生命周期:創建-DI-初始化-使用-銷毀
{}的內容最先輸出,其次是構造器,然后是賦值?
?
?1.bean 的生命周期(在單例模式下的生命周期)
(1)通過構造器創建 bean 的實例(執行類中無參構造方法去創建對象)
package com.lbj.spring5.bean;public class Orders {//為了明顯,我們寫一個無參構造方法public Orders(){System.out.println("第一步:執行無參構造創建bean實例");}private String oname;public void setOname(String oname) {this.oname = oname;} }?
(2)為 bean 的屬性設置值、或者對其他外部 bean 的引用(調用set 方法注入屬性)
xml
java
?
(3)調用 bean 的初始化方法(需要我們進行配置初始化的方法)
java
xml?
?
(4)bean 可以使用了(也就是說對象獲取到了)
java
?
(5)當容器關閉的時候,調用 bean 的銷毀的方法(需要進行配置銷毀的方法)
java
xml?
執行到這一步,其實并沒有讓bean 銷毀
需要手動讓bean 實例銷毀,意思是,如果我此時方法中不主動去調用銷毀方法,我的bean.xml配置文件中也不會執行銷毀
?
?TestSpring5Demo1的測試代碼如下:
package com.lbj.spring5.testdemo;import com.lbj.spring5.bean.Orders; import com.lbj.spring5.collectiontype.Book; import com.lbj.spring5.collectiontype.Course; import com.lbj.spring5.collectiontype.Student; import com.lbj.spring5.factorybean.Mybean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {@Testpublic void tsetBean1(){// ApplicationContext context= // new ClassPathXmlApplicationContext("bean4.xml");//使用的是ApplicationContext的子類進行調用close()方法,是一種向下轉型ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("bean4.xml");Orders orders=context.getBean("orders", Orders.class);System.out.println("第四步 獲取創建 bean 實例對象");System.out.println(orders); //5.手動讓bean 實例銷毀context.close();} }?
?
2.演示bean? 的生命周期
步驟1:創建一個bean包,并且創建一個Orders類
Orders類代碼如下:
package com.lbj.spring5.bean;public class Orders {//1.為了明顯,我們寫一個無參構造方法public Orders(){System.out.println("第一步:執行無參構造創建bean實例");}private String oname; //2.調用set 方法public void setOname(String oname) {this.oname = oname;System.out.println("第二步 調用 set 方法設置屬性值");}//3.創建執行的初始化方法,然后去bean中配置,讓這個普通的初始化方法執行public void initMethod(){System.out.println("第三步:執行初始化的方法");}// 5.創建銷毀的方法public void destoryMethod(){System.out.println("第五步:執行銷毀的方法");} }步驟2:新建一個bean4.xml
在bean4.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"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><bean id="orders" class="com.lbj.spring5.bean.Orders" init-method="initMethod" destroy-method="destoryMethod"><property name="oname" value="華為手機"></property> </bean> </beans>?
步驟3:測試方法調用以及手寫銷毀方法調用
package com.lbj.spring5.testdemo;import com.lbj.spring5.bean.Orders; import com.lbj.spring5.collectiontype.Book; import com.lbj.spring5.collectiontype.Course; import com.lbj.spring5.collectiontype.Student; import com.lbj.spring5.factorybean.Mybean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {@Testpublic void tsetBean1(){// ApplicationContext context= // new ClassPathXmlApplicationContext("bean4.xml");//使用的是ApplicationContext的子類進行調用close()方法,是一種向下轉型ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("bean4.xml");Orders orders=context.getBean("orders", Orders.class);System.out.println("第四步 獲取創建 bean 實例對象");System.out.println(orders); //5.手動讓bean 實例銷毀context.close();} }?
步驟4:實驗結果
?
?3.補充:bean 的后置處理器,bean的生命周期有七步
(1)通過構造器創建 bean 的實例(執行類中無參構造方法去創建對象)
(2)為 bean 的屬性設置值、或者對其他外部 bean 的引用(調用set 方法注入屬性)
(2.1)把 bean 實例傳遞 bean 后置處理器的方法?postProcessBeforeInitialization
(3)調用 bean 的初始化方法(需要我們進行配置初始化的方法)
(3.1)把bean 實例傳遞 bean 后置處理器的方法?postProcessAfterInitialization
(4)bean 可以使用了(也就是說對象獲取到了)
(5)當容器關閉的時候,調用 bean 的銷毀的方法(需要進行配置銷毀的方法)
4.演示添加后置處理器效果
(1)創建一個MyBeanPost 類,實現接口 BeanPostProcessor,創建后置處理器
按住ctrl,鼠標點擊接口就會進入到接口內查看接口中的抽象方法
進入接口后:
把接口的這兩個抽象方法復制出來到MyBeanPost 類
復制到MyBeanPost后,代碼如下:
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.lang.Nullable;public class MyBeanPost implements BeanPostProcessor{@Nullabledefault Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Nullabledefault Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;} }修改代碼后如下:
package com.lbj.spring5.bean;import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.lang.Nullable;public class MyBeanPost implements BeanPostProcessor{@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之前執行的方法");return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之后執行的方法");return bean;} }?
(2)上面的MyBeanPost類實例出來,但是就是一個普通類,咱們Spring 并不知道這是一個后置處理器,所以我們需要進行配置
bean4.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"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><bean id="orders" class="com.lbj.spring5.bean.Orders" init-method="initMethod" destroy-method="destoryMethod"><property name="oname" value="華為手機"></property> </bean><!--配置后置處理器--><!--這么做之后,我們的所有bean 實例都會自動添加上后置處理器--><!--MyBeanPost 類中的方法也會被執行--><bean id="myBeanPost" class="com.lbj.spring5.bean.MyBeanPost"></bean> </beans>?
(3)分析測試結果
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的IOC操作Bean管理XML方式(bean 的生命周期)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx的初步安装
- 下一篇: oracle rman备份spfile,