Spring入門(2)IOC賦值

2021-10-02 10:25:07 字數 3364 閱讀 2678

public

person

(string lastname, integer age, string gender)

使用constructor-arg來賦值

"person03"

class

="com.atguigu.bean.person"

>

<

!-- 呼叫有參構造器進行建立物件並賦值;掌握 --

>

"lastname" value=

"小明"

>

<

/constructor-arg>

"gender" value=

"男">

<

/constructor-arg>

"age" value=

"18"

>

<

/constructor-arg>

<

/bean>

**

public

class

person

package com.atguigu.bean;

public

class

carpublic string getcarname()

public

void

setcarname

(string carname)

public integer getprice()

public

void

setprice

(integer price)

public string getcolor()

public

void

setcolor

(string color)

@override

public string tostring()

}

賦值

"person01"

class

="com.atguigu.bean.person"

>

<

!-- lastname=

"null"

-->

"lastname"

>

<

!-- 進行複雜的賦值 --

>

>

<

/property>

<

!-- ref:代表引用外面的乙個值 ;引用其他bean car = ioc.

getbean

("car01")--

>

"car"

>

<

!--物件我們可以使用bean標籤建立 car =

newcar()

; 引用內部bean;不能被獲取到,只能內部使用 --

>

="com.atguigu.bean.car"

>

"carname" value=

"自行車"

>

<

/property>

<

/bean>

<

/property>

<

/bean>

private listbooks;

private mapmaps;

private properties properties;

"person02"

class

="com.atguigu.bean.person"

>

<

!-- 如何為list型別賦值 --

>

"books"

>

<

!-- books =

newarraylist

();--

>

<

!-- list標籤體中新增每乙個元素 --

>

="com.atguigu.bean.book" p:bookname=

"西遊記"

>

<

/bean>

<

!-- 引用外部乙個元素 --

>

"book01"

/>

<

/list>

<

/property>

<

!-- map

maps;

-->

"maps"

>

<

!-- maps =

newlinkedhashmap

<

>()

;-->

<

!-- 乙個entry代表乙個鍵值對 --

>

"key01" value=

"張三"

>

<

/entry>

"key02" value=

"18"

>

<

/entry>

"key03" value-ref=

"book01"

>

<

/entry>

"key04"

>

="com.atguigu.bean.car"

>

"carname" value=

"寶馬"

>

<

/property>

<

/bean>

<

/entry>

"key05"

>

李四<

/value>

<

/entry>

<

/map>

<

/property>

"properties"

>

<

!-- properties =

newproperties()

;所有的k=v都是string --

>

<

!-- k=v都是string;值直接寫在標籤體中 --

>

"username"

>root<

/prop>

"password"

>

123456

<

/prop>

<

/props>

<

/property>

<

/bean>

Spring基礎 2 IOC註解

xmlns xmlns xsi xmlns context xsi schemalocation spring beans.xsd spring context.xsd beans 掃瞄該包下的所有元件 component value userservice 相當於在xml的配置方式中 class ...

Spring學習(2)IoC之第乙個程式

ioc就是通過容器來實現物件的裝配與管理,當下比較流行的主要有兩種實現方式 依賴注入和依賴查詢。怎麼去理解依賴注入呢?也就是我們在邏輯 中假如要使用到乙個物件student,此時我們不用new乙個student,我們只需要在配置檔案中宣告載入到容器中,通過容器將student給呼叫者。接下來看看如何...

spring入門之IOC容器

ioc 其思想是反轉資源獲取的方向,傳統的資源查詢方式要求元件向容器發起請求查詢資源,作為回應,容器適時的返回資源 應用ioc後,容器主動地將資源推送給它所管理的元件,元件選擇一種合適的方式來接受資源 di 是ioc的另一種表達方式 即元件以一些預先定義好的方式 例如setter方法 接受來自容器的...