javascript
Spring教程--IOC(控制反转)详解
?IOC裝配Bean
1.1?Spring框架Bean實(shí)例化的方式
提供了三種方式實(shí)例化Bean.
* 構(gòu)造方法實(shí)例化:(默認(rèn)無參數(shù))
* 靜態(tài)工廠實(shí)例化:
* 實(shí)例工廠實(shí)例化:
1.1.1 無參數(shù)構(gòu)造方法的實(shí)例化:
<!-- 默認(rèn)情況下使用的就是無參數(shù)的構(gòu)造方法. -->
<bean id="bean1" class="com.sihai.spring3.demo2.Bean1"></bean>1.1.2 靜態(tài)工廠實(shí)例化:
<!-- 第二種使用靜態(tài)工廠實(shí)例化 -->
<bean id="bean2" class="com.sihai.spring3.demo2.Bean2Factory" factory-method="getBean2"></bean>1.1.3 實(shí)例工廠實(shí)例化:
<!-- 第三種使用實(shí)例工廠實(shí)例化 -->
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean><bean id="bean3Factory" class="com.sihai.spring3.demo2.Bean3Factory"/>1.2?Bean的其他配置
1.2.1 id和name的區(qū)別:
id遵守XML約束的id的約束.id約束保證這個(gè)屬性的值是唯一的,而且必須以字母開始,可以使用字母、數(shù)字、連字符、下劃線、句話、冒號(hào)
name沒有這些要求
***** 如果bean標(biāo)簽上沒有配置id,那么name可以作為id.
***** 開發(fā)中Spring和Struts1整合的時(shí)候, /login.
<bean name=”/login”?class=””>
現(xiàn)在的開發(fā)中都使用id屬性即可.
1.2.2 類的作用范圍:
scope屬性 :
* singleton:單例的.(默認(rèn)的值.)
* prototype:多例的.
* request:web開發(fā)中.創(chuàng)建了一個(gè)對象,將這個(gè)對象存入request范圍,request.setAttribute();
* session:web開發(fā)中.創(chuàng)建了一個(gè)對象,將這個(gè)對象存入session范圍,session.setAttribute();
* globalSession:一般用于Porlet應(yīng)用環(huán)境.指的是分布式開發(fā).不是porlet環(huán)境,globalSession等同于session;
實(shí)際開發(fā)中主要使用singleton,prototype
1.2.3 Bean的生命周期:
配置Bean的初始化和銷毀的方法:
配置初始化和銷毀的方法:
* init-method=”setup”
* destroy-method=”teardown”
執(zhí)行銷毀的時(shí)候,必須手動(dòng)關(guān)閉工廠,而且只對scope=”singleton”有效.
Bean的生命周期的11個(gè)步驟:
1.instantiate bean對象實(shí)例化
2.populate properties 封裝屬性
3.如果Bean實(shí)現(xiàn)BeanNameAware 執(zhí)行 setBeanName
4.如果Bean實(shí)現(xiàn)BeanFactoryAware 或者 ApplicationContextAware 設(shè)置工廠 setBeanFactory 或者上下文對象 setApplicationContext
5.如果存在類實(shí)現(xiàn) BeanPostProcessor(后處理Bean) ,執(zhí)行postProcessBeforeInitialization
6.如果Bean實(shí)現(xiàn)InitializingBean 執(zhí)行 afterPropertiesSet
7.調(diào)用<bean init-method="init"> 指定初始化方法 init
8.如果存在類實(shí)現(xiàn) BeanPostProcessor(處理Bean) ,執(zhí)行postProcessAfterInitialization
9.執(zhí)行業(yè)務(wù)處理
10.如果Bean實(shí)現(xiàn) DisposableBean 執(zhí)行 destroy
11.調(diào)用<bean destroy-method="customerDestroy"> 指定銷毀方法 customerDestroy
1.3?Bean中屬性注入
Spring支持構(gòu)造方法注入和setter方法注入:
1.3.1 構(gòu)造器注入:
<bean id="car" class="com.sihai.spring3.demo5.Car"><!-- <constructor-arg name="name" value="寶馬"/><constructor-arg name="price" value="1000000"/> --><constructor-arg index="0" type="java.lang.String" value="奔馳"/><constructor-arg index="1" type="java.lang.Double" value="2000000"/></bean>1.3.2 setter方法注入:
<bean id="car2" class="com.sihai.spring3.demo5.Car2"><!-- <property>標(biāo)簽中name就是屬性名稱,value是普通屬性的值,ref:引用其他的對象 --><property name="name" value="保時(shí)捷"/><property name="price" value="5000000"/></bean>1.3.3 setter方法注入對象屬性:
<property name="car2" ref="car2"/>1.3.4 名稱空間p:注入屬性:
Spring2.5版本引入了名稱空間p.
p:<屬性名>="xxx" 引入常量值
p:<屬性名>-ref="xxx" 引用其它Bean對象
引入名稱空間:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p"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.xsd"><bean id="car2" class="com.sihai.spring3.demo5.Car2" p:name="寶馬" p:price="400000"/><bean id="person" class="com.sihai.spring3.demo5.Person" p:name="童童" p:car2-ref="car2"/>1.3.5 SpEL:屬性的注入:
Spring3.0提供注入屬性方式:
語法:#{表達(dá)式}
<bean id="" value="#{表達(dá)式}">
<bean id="car2" class="com.sihai.spring3.demo5.Car2"><property name="name" value="#{'大眾'}"></property><property name="price" value="#{'120000'}"></property></bean><bean id="person" class="com.sihai.spring3.demo5.Person"><!--<property name="name" value="#{personInfo.name}"/>--><property name="name" value="#{personInfo.showName()}"/><property name="car2" value="#{car2}"/></bean><bean id="personInfo" class="com.sihai.spring3.demo5.PersonInfo"><property name="name" value="張三"/></bean>1.4?集合屬性的注入:
<bean id="collectionBean" class="com.sihai.spring3.demo6.CollectionBean"><!-- 注入List集合 --><property name="list"><list><value>童童</value><value>小鳳</value></list></property><!-- 注入set集合 --><property name="set"><set><value>杜宏</value><value>如花</value></set></property><!-- 注入map集合 --><property name="map"><map><entry key="剛剛" value="111"/><entry key="嬌嬌" value="333"/></map></property><property name="properties"><props><prop key="username">root</prop><prop key="password">123</prop></props></property></bean>1.5?加載配置文件:
一種寫法:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean1.xml",”bean2.xml”);
二種方法:
<import resource="applicationContext2.xml"/>
總結(jié)
以上是生活随笔為你收集整理的Spring教程--IOC(控制反转)详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring教程--入门程序
- 下一篇: Spring教程--IOC(注解方式)和