日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java程序员从笨鸟到菜鸟之(七十)细谈Spring(三)IOC和spring基本配置详解

發(fā)布時間:2025/3/21 java 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java程序员从笨鸟到菜鸟之(七十)细谈Spring(三)IOC和spring基本配置详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

對于IoC?的一些知識點,相信大家都知道他在spring框架中所占有的地位,應(yīng)該可以算的上是核心之一吧,所以IOC是否理解清楚,決定了大家對Spring整個框架的理解?

Ioc的理解

spring?的兩個核心概念:一個是控制反轉(zhuǎn)IoC,也可以叫做依賴注DI。還有一個是面向切面編程AOP

控制反轉(zhuǎn):當(dāng)某個Java?對象需要(依賴)另一個java?對象時,不

是自身直接創(chuàng)建依賴對象,而是由實現(xiàn)IoC?的容器(如spring?框架的IoC容器)來創(chuàng)建,并將它注入需要這個依賴對象的java?對象中。

spring?的容器

spring?管理的基本單元是Bean,在spring?的應(yīng)用中,所有的組件都是一個個的Bean,它可以是任何的java?對象。spring?負(fù)責(zé)創(chuàng)建這些Bean的實例。并管理生命周期。而spring?框架是通過其內(nèi)置的容器來完成Bean?的管理的,Bean?在spring?的容器中生存著,使用時只需要通過它提供的一些方法從其中獲取即可。

spring?的容器有兩個接口:BeanFactory?和ApplicationContext?這兩個接口的實例被陳為spring?的上下文。

[java]?view plaincopy print?
  • <span?style="font-size:18px;">ApplicationContext?ac?=?newClassFathXmlApplicationContext("app*.xml");??
  • AccountService?accountService?=(AccountService)ac.getBean("accountServiceImpl");</span>??


  • 注:由于ApplicationContext是基于BeanFactory之上的,所以,一般ApplicationContext功能比較強大,建議使用

    ApplicationContext經(jīng)常用到的三個實現(xiàn):

    1.ClassPathXmlApplicationContext:從類路徑中的XML文件載入上下文定義信息。把上下文定義文件當(dāng)成類路徑資源。

    2.FileSystemXmlApplicationContext:從文件系統(tǒng)中的XML文件載入上下文定義信息。

    3.XmlWebApplicationContext:從Web系統(tǒng)中的XML文件載入上下文定義信息。

    ?

    一:spring?的依賴注入

    1)、構(gòu)造器注入

    [html]?view plaincopy print?
  • <span?style="font-size:18px;"><bean?id="accoutDaoImpl"?class="cn.csdn.dao.AccoutDaoImpl"??
  • scope=”singleton”/>??
  • <bean?id="accoutServicImpl"??
  • class="cn.csdn.service.AccoutServicImpl"?scope=”">??
  • <!--?構(gòu)造方法注入方式-->??
  • <constructor-arg?ref="accoutDaoImpl"/>??
  • </bean></span>??

  • 這種注入方式很少用,如果是注入對象一般為上例注入,但有時要注入基本數(shù)據(jù)類型,一般用下面方法注入

    [html]?view plaincopy print?
  • <span?style="font-size:18px;"><constructor-arg>??
  • <value>hello?world!</value>??
  • </constructor-arg></span>??
  • 如果構(gòu)造方法不只一個參數(shù)時,應(yīng)指明所注入?yún)?shù)的索引或者數(shù)據(jù)類型,例如:

    [html]?view plaincopy print?
  • <span?style="font-size:18px;color:#333333;"><constructor-arg?index="0"?type="java.lang.String">??
  • <value>sunDriver</value>??
  • </constructor-arg>??
  • <constructor-arg?index="1"?type="java.lang.String">??
  • <value>jdbc:odbc:School</value>??
  • </constructor-arg></span>??
  • 2)、設(shè)值(set?方法)注入

    [html]?view plaincopy print?
  • <span?style="font-size:18px;"><bean?id="accountDaoImpl"??
  • class="cn.csdn.dao.AccoutDaoImpl"/>??
  • <bean?id="accoutServicImpl"??
  • class="cn.csdn.service.AccoutServicImpl">??
  • <!--?設(shè)值(set?方法)注入-->??
  • <property?name="accountDaoImpl"?ref="accoutDaoImpl"/>??
  • </bean></span>??

  • 注:<property?name="accountDaoImpl"?ref="accoutDaoImpl"/>

    相當(dāng)于調(diào)用set?AccountDaoImpl方法,把值設(shè)為accoutDaoImpl

    3)接口注入(很少用)

    二:?xml?裝配Bean屬性含義

    1.id:指定該Bean?的唯一標(biāo)識。

    2.class:指定該Bean?的全限定名。

    3.name:為該Bean?指定一到多個別名。多個別名可以用“,”和“;”分割。

    4.autowire:指定該Bean?的屬性的裝配方式。

    所謂自動裝配是指在<BEAN>標(biāo)簽中不用指定其依賴的BEAN,而是通過配置的自動裝配來自動注入依賴的BEAN,這種做法讓我們的配置更加簡單

    1no:不使用自動裝配。必須通過ref?元素指定依賴,這是默認(rèn)設(shè)置。由于顯式指定協(xié)作者可以使配置更靈活、更清晰,因此對于較大的部署配置,推薦采用該設(shè)置。而且在某種程度上,它也是系統(tǒng)架構(gòu)的一種文檔形式。

    [html]?view plaincopy print?
  • <span?style="font-size:18px;"><bean?id="bean1"?class="cn.csdn.service.Bean1"??
  • scope="singleton">??
  • <property?name="studentDaoImpl"??
  • ref="studentDaoImpl">??
  • </property>??
  • </bean></span>??

  • 備注:有property?屬性指定ref

    2byName:根據(jù)屬性名自動裝配。此選項將檢查容器并根據(jù)

    名字查找與屬性完全一致的bean,并將其與屬性自動裝配。例如,在

    bean?定義中將autowire?設(shè)置為by?name,而該bean?包含master?屬性(同時提供setMaster(..)方法),Spring?就會查找名為master?的bean?定義,并用它來裝配給master?屬性。

    <bean?id="bean1"?class="cn.csdn.service.Bean1"

    scope="singleton"?autowire="byName"/>

    備注:沒有property?屬性

    3byType:果容器中存在一個與指定屬性類型相同的

    bean,那么將與該屬性自動裝配。如果存在多個該類型的bean,那么

    將會拋出異常,并指出不能使用byType?方式進(jìn)行自動裝配。若沒有找到相匹配的bean,則什么事都不發(fā)生,屬性也不會被設(shè)置。如果你不希望這樣,那么可以通過設(shè)置dependency-check="objects"讓Spring?拋出異常。

    備注:spring3.0?以上不拋異常。

    <bean?id="bean1"?class="cn.csdn.service.Bean1"

    scope="singleton"?autowire="byType"/>

    備注:沒有property?屬性

    4Constructor:與byType?的方式類似,不同之處在于它應(yīng)用

    于構(gòu)造器參數(shù)。如果在容器中沒有找到與構(gòu)造器參數(shù)類型一致的bean,那么將會拋出異常。

    <bean?id="bean1"?class="cn.csdn.service.Bean1"

    scope="singleton"autowire="constructor"/>

    備注:沒有property?屬性

    5autodetect:通過bean?類的自省機(jī)制(introspection)來決定是使用constructor?還是byType?方式進(jìn)行自動裝配。如果發(fā)現(xiàn)默認(rèn)的

    構(gòu)造器,那么將使用byType?方式。

    <bean?id="bean1"?class="cn.csdn.service.Bean1"

    scope="singleton"?autowire="autodetect"/>

    5.scope:指定該Bean?的生存范圍

    scope用來聲明IOC容器中的對象應(yīng)該處的限定場景或者說該對象的存活空間,即在IOC容器在對象進(jìn)入相應(yīng)的scope之前,生成并裝配這些對象,在該對象不再處于這些scope的限定之后,容器通常會銷毀這些對象。

    1)?singleton類型bean定義,在一個容器中只存在一個實例,所有對該類型bean的依賴都引用這一單一實例

    2)?scopeprototypebean,容器在接受到該類型的對象的請求的時候,會每次都重新生成一?個新的對象給請求方,雖然這種類型的對象的實例化以及屬性設(shè)置等工作都是由容器負(fù)責(zé)的,但是只要準(zhǔn)備完畢,并且對象實例返回給請求方之后,容器就不在擁有?當(dāng)前對象的引用,請求方需要自己負(fù)責(zé)當(dāng)前對象后繼生命周期的管理工作,包括該對象的銷毀

    3)?request?sessionglobal?session

    這三個類型是spring2.0之后新增的,他們只適用于web程序,通常是和XmlWebApplicationContext共同使用

    request

    <bean?id?="requestPrecessor"?class="...RequestPrecessor"???scope="request"?/>

    Spring容器,即XmlWebApplicationContext?會為每個HTTP請求創(chuàng)建一個全新的RequestPrecessor對象,當(dāng)請求結(jié)束后,,該對象的生命周期即告結(jié)束

    session

    <bean?id?="userPreferences"?class="...UserPreferences"???scope="session"?/>

    Spring容器會為每個獨立的session創(chuàng)建屬于自己的全新的UserPreferences實例,他比request?scopebean會存活更長的時間,其他的方面真是沒什么區(qū)別。

    global?session:

    <bean?id?="userPreferences"?class="...UserPreferences"???scope="globalsession"?/>?

    global?session只有應(yīng)用在基于porlet的web應(yīng)用程序中才有意義,他映射到porlet的global范圍的session,如果普通的servlet的web?應(yīng)用中使用了這個scope,容器會把它作為普通的session的scope對待。

    6.init-method:指定該Bean?的初始化方法。destroy-method:指定該Bean?的銷毀方法。這個就像servletinitdestroy方法一樣,只不過這里在配置文件配置的

    7.abstract:指定該Bean?是否為抽象的。如果是抽象的,則

    spring?不為它創(chuàng)建實例。

    8.parent?

    如果兩個Bean?的屬性裝配信息很相似,那么可以利用繼

    承來減少重復(fù)的配置工作。

    <!--?裝配Bean?的繼承

    父類作為模板,不需要實例化,設(shè)置abstract=”true”-->

    `?<bean?id=”parent”?class=”cn.csdn.service.Parent”

    abstract=”true”>

    <property?name=”name”?value=”z_xiaofei168”/>

    <property?name=”pass”?value=”z_xiaofei168”/>

    </bean>

    <!--?裝配Bean?的繼承

    子類中用parent?屬性指定父類標(biāo)識或別名

    子類可以覆蓋父類的屬性裝配,也可以新增自己的屬性裝配

    -->

    `?<bean?id=”child”?class=”cn.csdn.service.Chlid”

    parent=”parent”>

    <property?name=”pass”?value=”123123”/>

    <property?name=”age”?value=”22”/>

    </bean>

    三:裝配Bean?的各種類型屬性值

    1..簡單類型屬性值的裝配

    [html]?view plaincopy print?
  • <span?style="font-size:18px;"><bean?id="bean1"?class="cn.csdn.domain.Bean1">??
  • <property?name="name"?value="z_xiaofei168"/>??
  • <property?name="age">??
  • <value>22</value>??
  • </property>??
  • </bean></span>??
  • 2.引用其他Bean?的裝配

    [html]?view plaincopy print?
  • <span?style="font-size:18px;"><bean?id="bean1"?class="cn.csdn.domain.Bean1">??
  • ...??
  • </bean>??
  • <bean?id="bean2"?class="cn.csdn.domain.Bean2">??
  • <!--?引用自其他Bean?的裝配-->??
  • <property?name="bean1"?ref="bean1"/>??
  • </bean></span>??

  • 另外一種不常使用的配置方式是在property?元素中嵌入

    一個bean?元素來指定所引用的Bean.

    [html]?view plaincopy print?
  • <span?style="font-size:18px;"><bean?id="bean1"?class="cn.csdn.domain.Bean1">??
  • ...??
  • </bean>??
  • <bean?id="bean2"?class="cn.csdn.domain.Bean2">??
  • <!--?引用自其他Bean?的裝配-->??
  • <property?name="bean1">??
  • <bean?id="bean1"??
  • class="cn.csdn.domain.Bean1"/>??
  • </property>??
  • </bean></span>??
  • 3.集合的裝配

    其實集合的裝配并不是復(fù)雜,反而感覺到很簡單,用一個例子來說明問題吧:


    [java]?view plaincopy print?
  • <span?style="font-size:18px;">package?com.bebig.dao.impl;??
  • import?java.util.List;??
  • import?java.util.Map;??
  • import?java.util.Properties;??
  • import?java.util.Set;??
  • import?com.bebig.dao.UserDAO;??
  • import?com.bebig.model.User;??
  • public?class?UserDAOImpl?implements?UserDAO?{??
  • ????private?Set<String>?sets;??
  • ????private?List<String>?lists;??
  • ????private?Map<String,?String>?maps;??
  • ????private?Properties?props;??
  • ????public?Set<String>?getSets()?{??
  • ????????return?sets;??
  • ????}??
  • ????public?void?setSets(Set<String>?sets)?{??
  • ????????this.sets?=?sets;?}??
  • ????public?List<String>?getLists()?{??
  • ????????return?lists;?}??
  • ????public?void?setLists(List<String>?lists)?{??
  • ????????this.lists?=?lists;??}??
  • ????public?Map<String,?String>?getMaps()?{??
  • ????????return?maps;??
  • ????}??
  • ????public?void?setMaps(Map<String,?String>?maps)?{??
  • ????????this.maps?=?maps;??
  • ????}??
  • ????public?Properties?getProps()?{??
  • ????????return?props;??
  • ????}??
  • ????public?void?setProps(Properties?props)?{??
  • ????????this.props?=?props;??
  • ????}??
  • ????public?void?save(User?u)?{??
  • ????????System.out.println("a?user?saved!");??
  • ????}??
  • ????@Override??
  • ????public?String?toString()?{??
  • ????????return?"sets.size:"?+?sets.size()?+?"?lists.size:"?+?lists.size()??
  • ????????????????+?"?maps.size:"?+?maps.size()?+?"?props.size:"?+?props.size();??
  • ????}??
  • }??
  • </span>??


  • 配置如下:

    [html]?view plaincopy print?
  • <span?style="font-size:18px;"><?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:context="http://www.springframework.org/schema/context"??
  • ?????>??
  • ????<!--?a?service?object;?we?will?be?profiling?its?methods?-->??
  • ????<bean?name="u"?class="com.bebig.dao.impl.UserDAOImpl">??
  • ????????<!--?set?-->??
  • ????????<property?name="sets">??
  • ????????????<set>??
  • ????????????????<value>1</value>??
  • ????????????????<value>2</value>??
  • ????????????</set>??
  • ????????</property>??
  • ????????<!--?list?-->??
  • ????????<property?name="lists">??
  • ????????????<list>??
  • ????????????????<value>a</value>??
  • ????????????????<value>b</value>??
  • ????????????</list>??
  • ????????</property>??
  • ????????<!--?map?-->??
  • ????????<property?name="maps">??
  • ????????????<map>??
  • ????????????????<entry?key="1"?value="aa"></entry>??
  • ????????????????<entry?key="2"?value="bb"></entry>??
  • ????????????</map>??
  • ????????</property>??
  • ????????<!--?properties?-->??
  • ????????<property?name="props">??
  • ????????????<props>??
  • ????????????????<prop?key="a">haha</prop>??
  • ????????????????<prop?key="b">hi</prop>??
  • ????????????</props>??
  • ????????</property>??
  • ????</bean>??
  • ????<bean?id="userService"?class="com.bebig.service.UserService"??
  • ????????scope="prototype">??
  • ????????<constructor-arg>??
  • ????????????<ref?bean="u"?/>??
  • ????????</constructor-arg>??
  • ????</bean>??
  • ????<!--?this?switches?on?the?load-time?weaving?-->??
  • ????<!--?<context:load-time-weaver?/>?-->??
  • </beans></span>??


  • 四:Spring?bean生命周期

    ? ??? 在傳統(tǒng)的Java應(yīng)用中,Bean的生命周期非常簡單。?Java的關(guān)鍵詞new用來實例化Bean(或許他是非序列化的)。這樣就夠用了。?相反,Bean的生命周期在Spring容器中更加細(xì)致。?理解Spring?Bean的生命周期非常重要,因為你或許要利用Spring提供的機(jī)會來訂制Bean的創(chuàng)建過程。?

    ?bean生命周期

    1.容器尋找Bean的定義信息并且將其實例化。?

    2.受用依賴注入,Spring按照Bean定義信息配置Bean的所有屬性。?

    3.如果Bean實現(xiàn)了BeanNameAware接口,工廠調(diào)用BeansetBeanName()方法傳遞BeanID。?

    4.如果Bean實現(xiàn)了BeanFactoryAware接口,工廠調(diào)用setBeanFactory()方法傳入工廠自身。?

    5.如果BeanPostProcessorBean關(guān)聯(lián),那么它們的postProcessBeforeInitialzation()方法將被調(diào)用。?

    6.如果Bean指定了init-method方法,它將被調(diào)用。?

    7.最后,如果有BeanPsotProcessorBean關(guān)聯(lián),那么它們的postProcessAfterInitialization()方法將被調(diào)用。?

    到這個時候,Bean已經(jīng)可以被應(yīng)用系統(tǒng)使用了,并且將被保留在Bean?Factory中知道它不再需要。?

    有兩種方法可以把它從Bean?Factory中刪除掉。?

    1.如果Bean實現(xiàn)了DisposableBean接口,destory()方法被調(diào)用。?

    2.如果指定了訂制的銷毀方法,就調(diào)用這個方法。?

    BeanSpring應(yīng)用上下文的生命周期與在Bean工廠中的生命周期只有一點不同,?唯一不同的是,如果Bean實現(xiàn)了ApplicationContextAwre接口,setApplicationContext()方法被調(diào)用。?

    只有singleton行為的bean接受容器管理生命周期。?

    non-singleton行為的beanSpring容器僅僅是new的替代,容器只負(fù)責(zé)創(chuàng)建。?

    對于singleton?beanSpring容器知道bean何時實例化結(jié)束,何時銷毀,?Spring可以管理實例化結(jié)束之后,和銷毀之前的行為,管理bean的生命周期行為主要未如下兩個時機(jī):?

    Bean全部依賴注入之后?

    Bean即將銷毀之前?

    1)依賴關(guān)系注入后的行為實現(xiàn):?

    有兩種方法:A.編寫init方法??B.實現(xiàn)InitializingBean接口?

    afterPropertiesSetinit同時出現(xiàn),前者先于后者執(zhí)行,使用init方法,需要對配置文件加入init-method屬性?

    2bean銷毀之前的行為?

    有兩種方法:A.編寫close方法??B.實現(xiàn)DisposableBean接口?

    destroyclose同時出現(xiàn),前者先于后者執(zhí)行,使用close方法,需要對配置文件加入destroy-method屬性?

    ?總體上分只為四個階段??

    ?1.?BeanFactoyPostProcessor實例化??

    ?2.?Bean實例化,然后通過某些BeanFactoyPostProcessor來進(jìn)行依賴注入??

    ?3.?BeanPostProcessor的調(diào)用.Spring內(nèi)置的BeanPostProcessor負(fù)責(zé)調(diào)用Bean實現(xiàn)的接口:?BeanNameAware,?BeanFactoryAware,?ApplicationContextAware等等,等這些內(nèi)置的BeanPostProcessor調(diào)用完后才會調(diào)用自己配置的BeanPostProcessor??

    ?4.Bean銷毀階段??

    注:xml依賴注入中的bean.xml例子:

    [html]?view plaincopy print?
  • <span?style="font-size:18px;"><?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-2.5.xsd">??
  • ??<bean?id="u"?class="com.bjsxt.dao.impl.UserDAOImpl">??
  • ??</bean>??
  • ??<bean?id="userService"?class="com.bjsxt.service.UserService">??
  • ????<!--??
  • ????<property?name="userDAO"?ref="u"?/>?
  • ?????-->??
  • ?????<constructor-arg>??
  • ????????<ref?bean="u"/>??
  • ?????</constructor-arg>??
  • ??</bean>??
  • </beans></span>??
  • 總結(jié)

    以上是生活随笔為你收集整理的Java程序员从笨鸟到菜鸟之(七十)细谈Spring(三)IOC和spring基本配置详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。