Bean的作用域

      網友投稿 930 2025-03-31

      Bean的作用域


      Bean的作用域

      默認情況下,所有Spring Bean都是單例的(整個Spring應用中,Bean的實例只有一個)。可以再元素中添加scope屬性來配置Spring Bean的作用范圍。

      scope作用域范圍:

      注意:在以上 6 種 Bean 作用域中,除了 singleton 和 prototype 可以直接在常規的 Spring IoC 容器(例如 ClassPathXmlApplicationContext)中使用外,剩下的都只能在基于 Web 的 ApplicationContext 實現(例如 XmlWebApplicationContext)中才能使用,否則就會拋出一個 IllegalStateException 的異常。

      單例模式singleton

      singleton是Spring Ioc容器默認的作用域,此時容器中只會存在一個共享的Bean實例。這個Bean實例會被存儲在高速緩存中,所有對于這個Bean的請求和引用,只要id與這個Bean定義相匹配,都會返回這個緩存中的對象實例。

      在 Spring 配置文件中,可以使用 元素的 scope 屬性,將 Bean 的作用域定義成 singleton,其配置方式如下所示:(不聲明scope屬性默認也是singleton)

      代碼示例:

      實體類

      package org.demo5; public class SingletonBean { private String str; public void setStr(String str) { this.str = str; } }

      xml配置文件

      main方法

      package org.demo5; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { //private static final Log LOGGER= LogFactory.getLog(MainApp.class); public static void main(String[] args) { //創建bean工廠 ApplicationContext context=new ClassPathXmlApplicationContext("demo5beans.xml"); //取bean SingletonBean sBean1=context.getBean("singletonBean",SingletonBean.class); SingletonBean sBean2=context.getBean("singletonBean",SingletonBean.class); System.out.println(sBean1); System.out.println(sBean2); } }

      運行結果:

      org.demo5.SingletonBean@696da30b org.demo5.SingletonBean@696da30b

      從控制臺的輸出可以看出,兩次獲得的 Bean 實例的地址完全一樣,這說明 IoC 容器只創建了一個 singletonBean 實例。由于 singleton 是 Spring IoC 容器的默認作用域,因此即使省略 scope 屬性,控制臺的輸出結果也一樣的。

      原型模式prototype

      當Bean定義的作用域為prototype,Spring容器會在每次請求該Bean時,都會創建一個新的Bean實例。

      代碼示例:

      實體類

      package org.demo5; public class PrototypeBean { private String str; public void setStr(String str) { this.str = str; } }

      XML配置文件

      main方法類

      package org.demo5;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { //private static final Log LOGGER= LogFactory.getLog(MainApp.class); public static void main(String[] args) { //創建bean工廠 ApplicationContext context=new ClassPathXmlApplicationContext("demo5beans.xml"); //singleton SingletonBean sBean1=context.getBean("singletonBean",SingletonBean.class); SingletonBean sBean2=context.getBean("singletonBean",SingletonBean.class); System.out.println(sBean1); System.out.println(sBean2); //prototype PrototypeBean pBean1=context.getBean("prototypeBean",PrototypeBean.class); PrototypeBean pBean2=context.getBean("prototypeBean",PrototypeBean.class); System.out.println(pBean1); System.out.println(pBean2); }}

      運行結果

      org.demo5.PrototypeBean@be35cd9 org.demo5.PrototypeBean@4944252c

      從運行結果可以看出,兩次輸出的內容并不相同,這說明在 prototype 作用域下,Spring 容器創建了兩個不同的 prototypeBean 實例。

      Spring web前端

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:Excel中SUMIF函數條件求和怎么使用
      下一篇:如何返回excel最大值所在行和內容
      相關文章
      久热综合在线亚洲精品| 国产精品亚洲色图| 国产亚洲情侣一区二区无| 亚洲精品中文字幕无码A片老| 亚洲av永久无码精品三区在线4| 亚洲资源在线观看| 亚洲午夜免费视频| 久久久久亚洲精品日久生情| 亚洲天天做日日做天天欢毛片| 久久精品国产亚洲AV高清热| 久久久亚洲欧洲日产国码aⅴ| 久久亚洲AV成人无码电影| 亚洲自偷自偷精品| 亚洲福利视频网站| www.亚洲成在线| 亚洲一区AV无码少妇电影| 亚洲人成网站免费播放| 噜噜综合亚洲AV中文无码| 国产成人亚洲午夜电影| 亚洲国产精品尤物yw在线| 亚洲欧洲中文日韩av乱码| 久久久久国产亚洲AV麻豆| 亚洲精品国产字幕久久不卡| 亚洲AV中文无码字幕色三| 亚洲人成网站在线播放影院在线 | 亚洲无人区午夜福利码高清完整版| 亚洲无线一二三四区手机| 伊人久久综在合线亚洲91| 亚洲精品国偷自产在线| 亚洲五月六月丁香激情| 亚洲制服在线观看| 亚洲欧美日韩中文高清www777| 亚洲av日韩aⅴ无码色老头| 成人亚洲网站www在线观看| 亚洲最大激情中文字幕| 亚洲av无码一区二区三区网站| 亚洲综合自拍成人| 亚洲五月综合网色九月色| 亚洲国产AV一区二区三区四区| 亚洲高清视频一视频二视频三| 国产精品亚洲二区在线观看 |