簡(jiǎn)單了解Spring IoC相關(guān)概念原理
Spring Ioc是Spring框架的基礎(chǔ),本文會(huì)簡(jiǎn)單的介紹下Spring Ioc。
Sprong Ioc即控制反轉(zhuǎn),它是一種通過(guò)描述(在java中可以是XML或注解)并通過(guò)第三方去產(chǎn)生或獲取特定對(duì)象的方式。
Spring IoC容器
1、Spring IoC容器的設(shè)計(jì)
Spring IoC容器的設(shè)計(jì)主要是基于BeanFactory和ApplicationContext這兩個(gè)接口,其中ApplicationContext是BeanFactory的一個(gè)子接口。也就是說(shuō),BeanFactory是Spring IoC容器定義的最底層接口,而ApplicationContext是其高級(jí)接口之一,因此大部分情況下會(huì)使用后者作為Spring IoC容器。
1.1 ClassPathXmlAppLicationContext
首先我們來(lái)認(rèn)識(shí)一下ApplicationContext的子類(lèi)ClassPathXmlAppLicationContext。先創(chuàng)建一個(gè).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 class='com.ssm.chapter.pojo.Source'> <property name='fruit' value='橙汁' /> <property name='sugar' value='少糖' /> <property name='size' value='大杯' /> </bean> <bean > <property name='beverageShop' value='貢茶' /> <property name='source' ref='source' /> </bean> </beans>
這里定義了兩個(gè)bean,這樣Spring IoC容器在初始化的時(shí)候就可以找到它們,然后使用ClassPathXmlAppLicationContext容器就可以將其初始化,代碼清單如下:
ApplicationContext ctx = new ClassPathXmlApplicationContext('spring-cfg.xml'); JuiceMaker juiceMaker = (JuiceMaker) ctx.getBean('juiceMaker'); System.out.println(juiceMaker.makeJuice());
這樣就會(huì)使用Application的實(shí)現(xiàn)類(lèi)ClassPathXmlAppLicationContext去初始化Spring IoC,然后開(kāi)發(fā)者就可以通過(guò)Ioc容器獲取資源了。
1.2 Spring Bean的生命周期
Spring IoC容器的本質(zhì)就是為了管理Bean。生命周期主要是為了了解Spring IoC容器初始化和銷(xiāo)毀Bean的過(guò)程,通過(guò)對(duì)它的學(xué)習(xí)就可以知道如何在初始和銷(xiāo)毀的時(shí)候加入自定義的方法,以滿足特定的需求。注:Spring IoC容器初始化和銷(xiāo)毀Bean的過(guò)程我這里就不介紹了啊,在網(wǎng)上很容易找到,這里主要是通過(guò)代碼去實(shí)現(xiàn)生命周期的過(guò)程。
除了了解生命周期的步驟之外,還要知道生命周期的接口是針對(duì)設(shè)么而言的,首先介紹生命周期的步驟:
①如果Bean實(shí)現(xiàn)了接口BeanNameAware,那么就會(huì)調(diào)用setBeanName方法。
②如果Bean實(shí)現(xiàn)了接口BeanFactoryAware,那么就會(huì)調(diào)用setBeanFactory方法。
③如果Bean實(shí)現(xiàn)了接口ApplicationContextAware,且Spring IoC容器也是ApplicationContext的一個(gè)實(shí)現(xiàn)類(lèi),那么就會(huì)調(diào)用setApplicationContext方法。
④如果Bean實(shí)現(xiàn)了接口BeanPostProcessor的,那么就會(huì)調(diào)用postProcessBeforeInitialization方法。
⑤如果Bean實(shí)現(xiàn)了接口BeanFactoryPostProcess,那么就會(huì)調(diào)用afterPropertiesSet方法。
⑥如果Bean自定義了初始化方法,它就會(huì)地用用已定義的初始化方法。
⑦如果Bean實(shí)現(xiàn)了接口BeanPostProcessor,那么就會(huì)調(diào)用postProcessAfterInitialization方法,之后這個(gè)bean就會(huì)完成了初始化,開(kāi)發(fā)者就可以從Spring IoC中獲取Bean的服務(wù)。
⑧如果Bean實(shí)現(xiàn)了接口DisposableBean,那么就會(huì)調(diào)用destroy的方法。
⑨如果定義了自定義銷(xiāo)毀方法,那么就會(huì)調(diào)用它。
此外,上面大部分的接口是針對(duì)單個(gè)Bean而言的;而B(niǎo)eanPostProcessor接口則是針對(duì)所有Bean而言的。為了測(cè)試BeanPostProcessor接口,可以寫(xiě)一個(gè)實(shí)現(xiàn)類(lèi):
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() + ']對(duì)象' + beanName + '開(kāi)始初始化'); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println('[' + bean.getClass().getSimpleName() + ']對(duì)象' + beanName + '實(shí)例化完成'); return bean; }}
這樣BeanPostProcessor就被我們用代碼實(shí)現(xiàn)了,他會(huì)處理Spring IoC容器中的所有Bean。
為了更好的展示生命周期的內(nèi)容,將上面的代碼中JuiceMaker類(lèi)進(jìn)行修改:
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í)行自定義銷(xiāo)毀方法'); } 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方法'); }}
這個(gè)類(lèi)實(shí)現(xiàn)了所以生命周期中的方法,以便以觀察生命周期,其中init方法是自定義的初始化方法,而myDestroy方法是自定義的銷(xiāo)毀方法,為了進(jìn)一步使用這兩個(gè)自定義方法,在描述Bean的時(shí)候,也要在.xml中進(jìn)行如下聲明:
<bean /> <bean class='com.ssm.chapter.pojo.Source'> <property name='fruit' value='橙汁' /> <property name='sugar' value='少糖' /> <property name='size' value='大杯' /> </bean> <bean init-method='init' destroy-method='myDestroy'> <property name='beverageShop' value='貢茶' /> <property name='source' ref='source' /> </bean>
這里定義了id為JuiceMaker的Bean,其屬性init-menth就是自定義的初始化方法,而destroy-method為自定義的銷(xiāo)毀方法。下面是測(cè)試代碼清單:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext('spring-cfg.xml'); JuiceMaker juiceMaker = (JuiceMaker) ctx.getBean('juiceMaker'); System.out.println(juiceMaker.makeJuice()); ctx.close();
日志如下:
[Source]對(duì)象source開(kāi)始初始化[Source]對(duì)象source實(shí)例化完成[JuiceMaker]調(diào)用BeanNameAware接口的setBeanName方法[JuiceMaker]調(diào)用BeanFactoryAware接口的setBeanFactory方法[JuiceMaker]調(diào)用ApplicationContextAware接口的setApplicationContext方法[JuiceMaker]對(duì)象juiceMaker開(kāi)始初始化[JuiceMaker]調(diào)用InitializingBean接口的afterPropertiesSet方法[JuiceMaker]執(zhí)行自定義初始化方法[JuiceMaker]對(duì)象juiceMaker實(shí)例化完成這是一杯由貢茶飲品店,提供的大杯少糖橙汁[JuiceMaker]調(diào)用DisposableBean接口的destroy方法[JuiceMaker]執(zhí)行自定義銷(xiāo)毀方法
從日志中可以看出,生命周期中的方法都被執(zhí)行了。也可以看到BeanPostProcessor針對(duì)的是全部Bean。我們也可以自定義初始化和銷(xiāo)毀Bean的方法。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 使用JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)秒表功能(含開(kāi)始、暫停、繼續(xù)、重置功能)2. Android簽名文件轉(zhuǎn)化為pk8和pem的實(shí)現(xiàn)3. PHP設(shè)計(jì)模式之迭代器模式Iterator實(shí)例分析【對(duì)象行為型】4. asp知識(shí)整理筆記4(問(wèn)答模式)5. layui Ajax請(qǐng)求給下拉框賦值的實(shí)例6. 詳解瀏覽器的緩存機(jī)制7. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算8. axios和ajax的區(qū)別點(diǎn)總結(jié)9. Android 項(xiàng)目正式簽名打包教程分享10. chat.asp聊天程序的編寫(xiě)方法
