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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring教程--IOC(控制反转)详解

發布時間:2025/3/20 javascript 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring教程--IOC(控制反转)详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?IOC裝配Bean

1.1?Spring框架Bean實例化的方式

提供了三種方式實例化Bean.

* 構造方法實例化:(默認無參數)

* 靜態工廠實例化:

* 實例工廠實例化:

1.1.1 無參數構造方法的實例化:

<!-- 默認情況下使用的就是無參數的構造方法. -->

<bean id="bean1" class="com.sihai.spring3.demo2.Bean1"></bean>

1.1.2 靜態工廠實例化:

<!-- 第二種使用靜態工廠實例化 -->

<bean id="bean2" class="com.sihai.spring3.demo2.Bean2Factory" factory-method="getBean2"></bean>

1.1.3 實例工廠實例化:

<!-- 第三種使用實例工廠實例化 -->

<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的區別:

id遵守XML約束的id的約束.id約束保證這個屬性的值是唯一的,而且必須以字母開始,可以使用字母、數字、連字符、下劃線、句話、冒號

name沒有這些要求

***** 如果bean標簽上沒有配置id,那么name可以作為id.

***** 開發中Spring和Struts1整合的時候, /login.

<bean name=”/login”?class=””>

現在的開發中都使用id屬性即可.

1.2.2 類的作用范圍:

scope屬性 :

* singleton:單例的.(默認的值.)

* prototype:多例的.

* request:web開發中.創建了一個對象,將這個對象存入request范圍,request.setAttribute();

* session:web開發中.創建了一個對象,將這個對象存入session范圍,session.setAttribute();

* globalSession:一般用于Porlet應用環境.指的是分布式開發.不是porlet環境,globalSession等同于session;

實際開發中主要使用singleton,prototype

1.2.3 Bean的生命周期:

配置Bean的初始化和銷毀的方法:

配置初始化和銷毀的方法:

* init-method=”setup”

* destroy-method=”teardown”

執行銷毀的時候,必須手動關閉工廠,而且只對scope=”singleton”有效.

Bean的生命周期的11個步驟:

1.instantiate bean對象實例化

2.populate properties 封裝屬性

3.如果Bean實現BeanNameAware 執行 setBeanName

4.如果Bean實現BeanFactoryAware 或者 ApplicationContextAware 設置工廠 setBeanFactory 或者上下文對象 setApplicationContext

5.如果存在類實現 BeanPostProcessor(后處理Bean) ,執行postProcessBeforeInitialization

6.如果Bean實現InitializingBean 執行 afterPropertiesSet

7.調用<bean init-method="init"> 指定初始化方法 init

8.如果存在類實現 BeanPostProcessor(處理Bean) ,執行postProcessAfterInitialization

9.執行業務處理

10.如果Bean實現 DisposableBean 執行 destroy

11.調用<bean destroy-method="customerDestroy"> 指定銷毀方法 customerDestroy

1.3?Bean中屬性注入

Spring支持構造方法注入和setter方法注入:

1.3.1 構造器注入:

<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>標簽中name就是屬性名稱,value是普通屬性的值,ref:引用其他的對象 --><property name="name" value="保時捷"/><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提供注入屬性方式:

語法:#{表達式}

<bean id="" value="#{表達式}">

<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"/>

總結

以上是生活随笔為你收集整理的Spring教程--IOC(控制反转)详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。