簡單了解Spring IoC相關(guān)概念原理
Spring Ioc是Spring框架的基礎,本文會簡單的介紹下Spring Ioc。
Sprong Ioc即控制反轉(zhuǎn),它是一種通過描述(在java中可以是XML或注解)并通過第三方去產(chǎn)生或獲取特定對象的方式。
Spring IoC容器
1、Spring IoC容器的設計
Spring IoC容器的設計主要是基于BeanFactory和ApplicationContext這兩個接口,其中ApplicationContext是BeanFactory的一個子接口。也就是說,BeanFactory是Spring IoC容器定義的最底層接口,而ApplicationContext是其高級接口之一,因此大部分情況下會使用后者作為Spring IoC容器。
1.1 ClassPathXmlAppLicationContext
首先我們來認識一下ApplicationContext的子類ClassPathXmlAppLicationContext。先創(chuàng)建一個.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-4.0.xsd"> <bean id="source" class="com.ssm.chapter.pojo.Source"> <property name="fruit" value="橙汁" /> <property name="sugar" value="少糖" /> <property name="size" value="大杯" /> </bean> <bean id="juiceMaker" class="com.ssm.chapter.pojo.JuiceMaker" > <property name="beverageShop" value="貢茶" /> <property name="source" ref="source" /> </bean> </beans>
這里定義了兩個bean,這樣Spring IoC容器在初始化的時候就可以找到它們,然后使用ClassPathXmlAppLicationContext容器就可以將其初始化,代碼清單如下:
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cfg.xml"); JuiceMaker juiceMaker = (JuiceMaker) ctx.getBean("juiceMaker"); System.out.println(juiceMaker.makeJuice());
這樣就會使用Application的實現(xiàn)類ClassPathXmlAppLicationContext去初始化Spring IoC,然后開發(fā)者就可以通過Ioc容器獲取資源了。
1.2 Spring Bean的生命周期
Spring IoC容器的本質(zhì)就是為了管理Bean。生命周期主要是為了了解Spring IoC容器初始化和銷毀Bean的過程,通過對它的學習就可以知道如何在初始和銷毀的時候加入自定義的方法,以滿足特定的需求。注:Spring IoC容器初始化和銷毀Bean的過程我這里就不介紹了啊,在網(wǎng)上很容易找到,這里主要是通過代碼去實現(xiàn)生命周期的過程。
除了了解生命周期的步驟之外,還要知道生命周期的接口是針對設么而言的,首先介紹生命周期的步驟:
①如果Bean實現(xiàn)了接口BeanNameAware,那么就會調(diào)用setBeanName方法。
②如果Bean實現(xiàn)了接口BeanFactoryAware,那么就會調(diào)用setBeanFactory方法。
③如果Bean實現(xiàn)了接口ApplicationContextAware,且Spring IoC容器也是ApplicationContext的一個實現(xiàn)類,那么就會調(diào)用setApplicationContext方法。
④如果Bean實現(xiàn)了接口BeanPostProcessor的,那么就會調(diào)用postProcessBeforeInitialization方法。
⑤如果Bean實現(xiàn)了接口BeanFactoryPostProcess,那么就會調(diào)用afterPropertiesSet方法。
⑥如果Bean自定義了初始化方法,它就會地用用已定義的初始化方法。
⑦如果Bean實現(xiàn)了接口BeanPostProcessor,那么就會調(diào)用postProcessAfterInitialization方法,之后這個bean就會完成了初始化,開發(fā)者就可以從Spring IoC中獲取Bean的服務。
⑧如果Bean實現(xiàn)了接口DisposableBean,那么就會調(diào)用destroy的方法。
⑨如果定義了自定義銷毀方法,那么就會調(diào)用它。
此外,上面大部分的接口是針對單個Bean而言的;而BeanPostProcessor接口則是針對所有Bean而言的。為了測試BeanPostProcessor接口,可以寫一個實現(xiàn)類:
package com.ssm.chapter.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class BeanPostProcessorImpl implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("[" + bean.getClass().getSimpleName() + "]對象" + beanName + "開始初始化"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("[" + bean.getClass().getSimpleName() + "]對象" + beanName + "實例化完成"); return bean; } }
這樣BeanPostProcessor就被我們用代碼實現(xiàn)了,他會處理Spring IoC容器中的所有Bean。
為了更好的展示生命周期的內(nèi)容,將上面的代碼中JuiceMaker類進行修改:
package com.ssm.chapter.pojo; 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 JuiceMaker implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean{ private String beverageShop = null; private Source source = null; public String getBeverageShop() { return beverageShop; } public void setBeverageShop(String beverageShop) { this.beverageShop = beverageShop; } public Source getSource() { return source; } public void setSource(Source source) { this.source = source; } public void init() { System.out.println("[" + this.getClass().getSimpleName() + "]執(zhí)行自定義初始化方法"); } public void myDestroy() { System.out.println("[" + this.getClass().getSimpleName() + "]執(zhí)行自定義銷毀方法"); } public String makeJuice() { String juice = "這是一杯由" + beverageShop + "飲品店,提供的" + source.getSize() +source.getSugar() + source.getFruit(); return juice; } @Override public void setBeanName(String name) { System.out.println("[" + this.getClass().getSimpleName() + "]調(diào)用BeanNameAware接口的setBeanName方法"); } @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("[" + this.getClass().getSimpleName() + "]調(diào)用BeanFactoryAware接口的setBeanFactory方法"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("[" + this.getClass().getSimpleName() + "]調(diào)用ApplicationContextAware接口的setApplicationContext方法"); } @Override public void destroy() throws Exception { System.out.println("[" + this.getClass().getSimpleName() + "]調(diào)用DisposableBean接口的destroy方法"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("[" + this.getClass().getSimpleName() + "]調(diào)用InitializingBean接口的afterPropertiesSet方法"); } }
這個類實現(xiàn)了所以生命周期中的方法,以便以觀察生命周期,其中init方法是自定義的初始化方法,而myDestroy方法是自定義的銷毀方法,為了進一步使用這兩個自定義方法,在描述Bean的時候,也要在.xml中進行如下聲明:
<bean id="beanPostProcessor" class="com.ssm.chapter.bean.BeanPostProcessorImpl" /> <bean id="source" class="com.ssm.chapter.pojo.Source"> <property name="fruit" value="橙汁" /> <property name="sugar" value="少糖" /> <property name="size" value="大杯" /> </bean> <bean id="juiceMaker" class="com.ssm.chapter.pojo.JuiceMaker" init-method="init" destroy-method="myDestroy"> <property name="beverageShop" value="貢茶" /> <property name="source" ref="source" /> </bean>
這里定義了id為JuiceMaker的Bean,其屬性init-menth就是自定義的初始化方法,而destroy-method為自定義的銷毀方法。下面是測試代碼清單:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-cfg.xml"); JuiceMaker juiceMaker = (JuiceMaker) ctx.getBean("juiceMaker"); System.out.println(juiceMaker.makeJuice()); ctx.close();
日志如下:
[Source]對象source開始初始化
[Source]對象source實例化完成
[JuiceMaker]調(diào)用BeanNameAware接口的setBeanName方法
[JuiceMaker]調(diào)用BeanFactoryAware接口的setBeanFactory方法
[JuiceMaker]調(diào)用ApplicationContextAware接口的setApplicationContext方法
[JuiceMaker]對象juiceMaker開始初始化
[JuiceMaker]調(diào)用InitializingBean接口的afterPropertiesSet方法
[JuiceMaker]執(zhí)行自定義初始化方法
[JuiceMaker]對象juiceMaker實例化完成
這是一杯由貢茶飲品店,提供的大杯少糖橙汁
[JuiceMaker]調(diào)用DisposableBean接口的destroy方法
[JuiceMaker]執(zhí)行自定義銷毀方法
從日志中可以看出,生命周期中的方法都被執(zhí)行了。也可以看到BeanPostProcessor針對的是全部Bean。我們也可以自定義初始化和銷毀Bean的方法。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
sprintboot使用spring-security包,緩存內(nèi)存與redis共存方式
這篇文章主要介紹了sprintboot使用spring-security包,緩存內(nèi)存與redis共存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10springboot整合redisson實現(xiàn)延時隊列(附倉庫地址)
延時隊列用于管理需要定時執(zhí)行的任務,對于大數(shù)據(jù)量和高實時性需求,使用延時隊列比定時掃庫更高效,Redisson提供一種高效的延時隊列實現(xiàn)方式,本文就來詳細的介紹一下,感興趣都可以了解學習2024-10-10如何使用Jackson和JSON Pointer查詢解析任何JSON節(jié)點
本文介紹了JSON Pointer是字符串表達式,可以非常方便解析復雜JSON節(jié)點值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Spring ApplicationListener監(jiān)聽器用法詳解
這篇文章主要介紹了Spring ApplicationListener監(jiān)聽器用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11