Spring系列筆記 3 Bean標籤屬性詳解

2021-10-08 02:32:55 字數 2401 閱讀 6828

如果痛苦無法避免,那麼就燃起鬥志去直面它!

說在前頭:

bean標籤內的屬性:

"testbean"

class

="com.example.bean.testbean"

>

"name" value=

"hello world!"

/>

<

/bean>

="com.example.bean.testbean" scope=

"singleton或者prototype"

>

"name" value=

"hello world!"

/>

<

/bean>

new

("beans.xml");

//如果scope="singleton"則每次的testbean物件都是相同的

//如果scope="prototype"則每次的testbean物件都不相同

testbean testbean =

(testbean) context.

getbean

("testbean");

"testbean"

class

="com.example.bean.testbean" init-method=

"init" destroy-method=

"destroy"

>

"name" value=

"hello world!"

/>

<

/bean>

public

class

testbean

public

void

setname

(string name)

public

void

init()

public

void

destroy()

}

public

class

}

執行結果:

testbean物件被初始化

hello world!

testbean物件被銷毀

package com.example.bean;

public

class

parent

public

void

setfirstname

(string firstname)

public string getlastname()

public

void

setlastname

(string lastname)

@override

public string tostring()

}

package com.example.bean;

public

class

child

public

void

setfirstname

(string firstname)

public string getlastname()

public

void

setlastname

(string lastname)

@override

public string tostring()

}

"parent"

class

="com.example.bean.parent"

>

"firstname" value=

"hua"

/>

"lastname" value=

"li"

/>

<

/bean>

"child"

class

="com.example.bean.child" parent=

"parent"

>

"firstname" value=

"ming"

/>

<

/bean>

public

class

}

i am lihua

i am liming

Spring 學習筆記 (三) Bean

配置 例項化 裝配 注入不同資料型別 基於annotation 定義 bean 標註在實現類上 bean 元件裝配 標註在屬性變數,屬性的 setter 方法,構造方法上 載入註解定義的 bean 區別見 difference between contextannotation config and...

Spring 3 自動裝配bean

class com.csu.autowire.address p city changsha p street shaoshan id car class com.csu.factory.car p name benz p price 632275 p speed 290 p address ref...

Spring載入Bean定義檔案筆記

載入xml定義檔案方式四種 beanfactory bf new xmlbeanfactory new classpathresource spring.xml 意思是beanfactory從xml讀取bean定義的資訊,現在bean工廠還沒有例項化bean,bean是延遲載入到bean工廠中,也就...