javascript
@kafkalistener中id的作用_SSM框架(十一):Spring框架中的IoC(1)
控制反轉(zhuǎn) IOC:
控制反轉(zhuǎn)(Inversion of Control,縮寫為IoC),是面向?qū)ο缶幊讨械囊环N設(shè)計(jì)原則,可以用來減低計(jì)算機(jī)代碼之間的耦合度。其中最常見的方式叫做依賴注入(Dependency Injection,簡(jiǎn)稱DI),還有一種方式叫“依賴查找”(Dependency Lookup)。
在bean.xml中加入bean標(biāo)簽
<!--把對(duì)象的創(chuàng)建交給spring來管理--><bean id="accountService" class="cn.figo.service.impl.AccountServiceImpl"></bean><bean id="accountDao" class="cn.figo.dao.impl.AccountDaoImpl"></bean>然后可以獲取spring的Ioc核心容器,并根據(jù)id獲取bean對(duì)象
// 獲取核心容器對(duì)象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");// 根據(jù)id獲取bean對(duì)象IAccountService accountService = (IAccountService) ac.getBean("accountService");IAccountDao accountDao = (IAccountDao) ac.getBean("accountDao");核心容器的繼承關(guān)系:
ApplicationContext 的父類和實(shí)現(xiàn)類ApplicationContext的三個(gè)常用實(shí)現(xiàn)類:
ClassPathXmlApplicationContext:它可以加載類路徑下的配置文件,要求配置文件必須在類路徑下。不在的話,加載不了。(更常用)
FileSystemXmlApplicationContext:它可以加載磁盤任意路徑下的配置文件(必須有訪問權(quán)限)
AnnotationConfigApplicationContext:它是用于讀取注解創(chuàng)建容器的
核心容器的兩個(gè)接口
ApplicationContext: 單例對(duì)象適用,實(shí)際開發(fā)通常采用此接口
它在構(gòu)建核心容器時(shí),創(chuàng)建對(duì)象采取的策略是采用立即加載的方式。也就是說,只要一讀取完配置文件馬上就創(chuàng)建配置文件中配置的對(duì)象。
ApplicationContext 立即加載BeanFactory: 多例對(duì)象使用
它在構(gòu)建核心容器時(shí),創(chuàng)建對(duì)象采取的策略是采用延遲加載的方式。也就是說,什么時(shí)候根據(jù)id獲取對(duì)象了,什么時(shí)候才真正的創(chuàng)建對(duì)象。
BeanFactory 延遲加載spring對(duì)bean的管理
第一種方式:使用默認(rèn)構(gòu)造函數(shù)創(chuàng)建。
在spring的配置文件中使用bean標(biāo)簽,配以id和class屬性之后,且沒有其他屬性和標(biāo)簽時(shí)。
<bean id="accountService" class="cn.figo.service.impl.AccountServiceImpl"></bean>采用的就是默認(rèn)構(gòu)造函數(shù)創(chuàng)建bean對(duì)象,此時(shí)如果類中沒有默認(rèn)構(gòu)造函數(shù),則對(duì)象無法創(chuàng)建。
這里如果我們覆蓋掉默認(rèn)構(gòu)造函數(shù)后,運(yùn)行會(huì)報(bào)錯(cuò)
public AccountServiceImpl(String id){System.out.println("create service");}第二種方式: 使用普通工廠中的方法創(chuàng)建對(duì)象(使用某個(gè)類中的方法創(chuàng)建對(duì)象,并存入spring容器)
通過某一個(gè)其他類中的一個(gè)方法來創(chuàng)建我們所需要的bean對(duì)象,比如這里有一個(gè)類InstanceFactory 中的方法 getAccountService()可以創(chuàng)建 IAccountService 對(duì)象
/*** 模擬一個(gè)工廠類(該類可能是存在于jar包中的,我們無法通過修改源碼的方式來提供默認(rèn)構(gòu)造函數(shù))*/ public class InstanceFactory {public IAccountService getAccountService(){return new AccountServiceImpl();} }在 xml中做如下配置
<bean id="instanceFactory" class="cn.figo.factory.InstanceFactory"></bean> <bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>第三種方式:使用工廠中的靜態(tài)方法創(chuàng)建對(duì)象(使用某個(gè)類中的靜態(tài)方法創(chuàng)建對(duì)象,并存入spring容器)
通過某一個(gè)其他類中的靜態(tài)方法來創(chuàng)建我們所需要的bean對(duì)象,比如這里有一個(gè)類StaticFactory 中的方法 getAccountService()可以創(chuàng)建 IAccountService 對(duì)象
/*** 模擬一個(gè)工廠類(該類可能是存在于jar包中的,我們無法通過修改源碼的方式來提供默認(rèn)構(gòu)造函數(shù))*/ public class StaticFactory {public static IAccountService getAccountService(){return new AccountServiceImpl();} }在xml中做如下配置:
<bean id="accountService" class="cn.figo.factory.StaticFactory" factory-method="getAccountService"></bean>這樣就可以成功創(chuàng)建相應(yīng)的bean對(duì)象了
bean的作用范圍調(diào)整
bean標(biāo)簽的scope屬性:
作用:用于指定bean的作用范圍
取值: 常用的就是單例的和多例的
singleton:單例的(默認(rèn)值)
prototype:多例的
request:作用于web應(yīng)用的請(qǐng)求范圍
session:作用于web應(yīng)用的會(huì)話范圍
global-session:作用于集群環(huán)境的會(huì)話范圍(全局會(huì)話范圍),當(dāng)不是集群環(huán)境時(shí),它就是session
當(dāng)設(shè)置bean標(biāo)簽的scope為prototype時(shí),是多例的
<bean id="accountService" class="cn.figo.service.impl.AccountServiceImpl" scope="prototype"></bean>測(cè)試一下:
// 獲取核心容器對(duì)象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");// 根據(jù)id獲取bean對(duì)象IAccountDao accountDao = (IAccountDao) ac.getBean("accountDao");System.out.println(accountDao);IAccountService accountService = (IAccountService) ac.getBean("accountService");IAccountService accountService2 = (IAccountService) ac.getBean("accountService");System.out.println(accountService);System.out.println(accountService2);System.out.println(accountService == accountService2);當(dāng)設(shè)置bean標(biāo)簽的scope為singleton時(shí),是單例的
bean對(duì)象的生命周期
單例對(duì)象:單例對(duì)象的生命周期和容器相同
出生:當(dāng)容器創(chuàng)建時(shí)對(duì)象出生
活著:只要容器還在,對(duì)象一直活著
死亡:容器銷毀,對(duì)象消亡
多例對(duì)象
出生:當(dāng)我們使用對(duì)象時(shí)spring框架為我們創(chuàng)建
活著:對(duì)象只要是在使用過程中就一直活著。
死亡:當(dāng)對(duì)象長(zhǎng)時(shí)間不用,且沒有別的對(duì)象引用時(shí),由Java的垃圾回收器回收
在類中加入 init 和 destroy 方法
public class AccountServiceImpl implements IAccountService {private IAccountDao accountDao = new AccountDaoImpl();public AccountServiceImpl(){System.out.println("對(duì)象創(chuàng)建了");}public void saveAccount(){System.out.println("service中的saveAccount方法執(zhí)行了。。。");}public void init(){System.out.println("對(duì)象初始化了。。。");}public void destroy(){System.out.println("對(duì)象銷毀了。。。");} }在 xml中配置 初始化方法和銷毀方法
<bean id="accountService" class="cn.figo.service.impl.AccountServiceImpl"scope="singleton" init-method="init" destroy-method="destroy"></bean>然后測(cè)試一下
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountService accountService = (IAccountService) ac.getBean("accountService"); accountService.saveAccount(); System.out.println(accountService); ac.close();單例時(shí):
單例時(shí) 對(duì)象隨容器一起銷毀多例時(shí):
多例時(shí) 即使容器關(guān)閉,對(duì)象也沒有被銷毀總結(jié)
以上是生活随笔為你收集整理的@kafkalistener中id的作用_SSM框架(十一):Spring框架中的IoC(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pcdmis怎么导出模型_从代数几何到导
- 下一篇: ioc spring 上机案例_抛开Sp