id="greeting"
class
="lm.proctice.spring.demo.impl.helloserviceimpl"
scope
="prototype"
>
......
1.id屬性
id屬性用來唯一標識標籤,是標籤中的最基本屬性。
2.class屬性
用來表示類的全名,通常為package.classname,本例中,我的package為lm.proctice.spring.demo.impl,類名為helloserviceimpl
3.scope屬性
預設情況下,如果不設定scope屬性,那麼預設為singleton,即單例項模式,也就是說對於同乙個bean,多次呼叫getbean方法返回的都是同乙個bean物件,例如下面的**輸出相同的結果:
system.out輸出:.println(context.getbean("greeting").hashcode());
system.out
.println(context.getbean("greeting").hashcode());
604378607如果指明scope的值,如本例中指定為prototype,那麼當每次的使用getbean方法時都會獲得乙個新的bean物件,這樣再執行下面的兩行**時,就有不同的輸出:604378607
system.out輸出:.println(context.getbean("greeting").hashcode());
system.out
.println(context.getbean("greeting").hashcode());
749550947563692927
4.name屬性
除了例子中給出的三個屬性外,標籤還有乙個name屬性比較常見,該屬性主要是設定標籤的別名,可以使用name屬性來取代id屬性
例如給本例中的標籤設定乙個name:
id在獲得裝配物件helloservice時可以寫成:="greeting" name="one,two three;"
class
="lm.proctice.spring.demo.impl.helloserviceimpl"
scope
="prototype"
>
......
helloservice hello=(helloservice)context.getbean("one");使用name的值
注:如果有多個別名(允許有多個別名),通過逗號(,),空格和分號(;)來分隔,也可以混合使用,例如例子中的寫法。
Spring bean標籤的常用屬性
id id標籤是bean的唯一標識,ioc容器中bean的id標籤不能重複,否則報錯。name bean的名稱識別符號,跟id一樣不能重複,否則會報錯。class 定義bean的類路徑,只想classpath下類所在的位置。factory method 工廠方法屬性,通過該屬性可以呼叫工廠指定的靜態...
spring bean標籤上的常用屬性
bean標籤的唯一識別符號,在 spring 容器中,只能存在唯一的 id不能重複,否則報錯。datasource name ds,ds2 class com.alibaba.druid.pool.druiddatasource bean druiddatasource datasource ac....
簡單了解Spring Bean常用註解的裝配
基於註解的裝配 在spring框架中,儘管使用xml配置檔案可以很簡單地裝配bean,但如果應用中有大量的bean需要裝配,會導致xml配置檔案過於龐大,不方便以後的公升級與維護,因此更多的時候推薦開發者使用註解hduok annotation 的方式去裝配bean。在spring框架中定義了一系列...