Spring學習(4) DI依賴注入

2021-10-08 23:58:09 字數 3830 閱讀 6047

實體pojo類

public

class

address

public

void

setaddress

(string address)

@override

public string tostring()

';}}

public

class

student

public

void

setname

(string name)

public address getaddress()

public

void

setaddress

(address address)

public string[

]getbooks()

public

void

setbooks

(string[

] books)

public list

gethobbies()

public

void

sethobbies

(list

hobbies)

public map

getcard()

public

void

setcard

(map

card)

public set

getgames()

public

void

setgames

(set

games)

public properties getinfo()

public

void

setinfo

(properties info)

@override

public string tostring()

';}}

<?xml version="1.0" encoding="utf-8"?>

xmlns

=""xmlns:xsi

=""xsi:schemalocation

="/spring-beans.xsd"

>

"addr"

class

="site.duing.pojo.address"

>

name

="address"

value

="中國"

/>

bean

>

"student"

class

="site.duing.pojo.student"

>

name

="name"

value

="李磊"

/>

name

="address"

ref="addr"

/>

name

="books"

>

>

>

紅樓夢value

>

>

西遊記value

>

>

水滸傳value

>

>

三國演義value

>

array

>

property

>

name

="hobbies"

>

>

>

聽歌value

>

>

敲**value

>

>

看電影value

>

list

>

property

>

name

="card"

>

>

key=

"身份證"

value

="123456789"

/>

key=

"銀行卡"

value

="987654321"

/>

map>

property

>

name

="games"

>

>

>

cfvalue

>

>

lolvalue

>

>

csvalue

>

set>

property

>

name

="info"

>

>

key=

"學號"

>

0123456prop

>

key=

"性別"

>

男prop

="小飛"

p:age

="20"

/>

"user2"

class

="site.duing.pojo.user"

c:name

="大飛"

p:age

="22"

/>

singleton(預設)

"user"

class

="site.duing.pojo.user"

scope

="singleton"

>

@test

public

void

test03()

結果為true

prototype

"user"

class

="site.duing.pojo.user"

scope

="prototype"

/>

四 Spring4學習 依賴注入DI

依賴 指bean物件建立依賴於容器。bean物件的依賴資源 注入 指bean物件依賴的資源由容器來設定和裝配 di dependency injection ioc 的另一種表述方式 即元件以一些預先定義好的方式 例如 setter 方法 接受來自如容器的資源注入.相對於 ioc 而言,這種表述更直...

Spring依賴注入(DI)

在spring框架中,依賴注入 di 的設計模式是用來定義物件彼此間的依賴。它主要有兩種型別 這是最流行最簡單的di注入方法,通過設定方法注入依賴。示例 幫助器類和指定的setter方法 package com.yiibai.output import com.yiibai.output.ioutp...

Spring依賴注入 DI

在我們的pojo類中需要get和set方法 public class student public void setname string name public address getaddress public void setaddress address address public str...