日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
詳解Spring中bean的作用域

Spring中bean的作用域

創(chuàng)新互聯(lián)建站是一家專(zhuān)注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),黃南州網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:黃南州等地區(qū)。黃南州做網(wǎng)站價(jià)格咨詢(xún):18982081108

Spring中bean的作用域用scope來(lái)表示。

scope的值有兩個(gè):

singleton為單粒,即Spring IoC容器只會(huì)創(chuàng)建該bean的***一個(gè)實(shí)例,這也是默認(rèn)的。該實(shí)例就會(huì)一直放在緩存里供大家使用。

prototype為原型,即每一次請(qǐng)求都會(huì)產(chǎn)生一個(gè)新的bean實(shí)例。

以實(shí)例說(shuō)明

Bean

Java代碼

 
 
 
  1. package com.cos.bean110317;     
  2.     
  3. public class Bean1 {     
  4.     
  5.     private String name;     
  6.     
  7.     public String getName() {     
  8.         return name;     
  9.     }     
  10.     
  11.     public void setName(String name) {     
  12.         this.name = name;     
  13.     }     
  14. }    

applicationContext_5.xml

Xml代碼

 
 
 
  1. "1.0" encoding="UTF-8"?>    
  2.     
  3. "http://www.springframework.org/schema/beans"    
  4.     xmlns:amq="http://activemq.apache.org/schema/core" xmlns:jms="http://www.springframework.org/schema/jms"    
  5.     xmlns:context="http://www.springframework.org/schema/context"    
  6.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  7.     xsi:schemaLocation="     
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  9. http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.4.0.xsd     
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     
  11. http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">    
  12.     
  13.      "bean1" class="com.cos.bean110317.Bean1" scope="singleton">    
  14.          
  15.          "name" value="zhangsan"/>    
  16.         
  17.    

當(dāng)scope不寫(xiě)的時(shí)候默認(rèn)為singleton

當(dāng)scope為singleton的時(shí)候,輸出結(jié)果bean11 == bean12

當(dāng)scope為prototype的時(shí)候,輸出結(jié)果bean11 != bean12

測(cè)試類(lèi)

Java代碼

 
 
 
  1. package com.cos.bean110317;     
  2.     
  3. import org.springframework.beans.factory.BeanFactory;     
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;     
  5.     
  6. public class Test {     
  7.     
  8.     public static void main(String[] args) {     
  9.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext_5.xml");     
  10.         Bean1 bean11 = (Bean1) factory.getBean("bean1");     
  11.         Bean1 bean12 = (Bean1) factory.getBean("bean1");     
  12.         if(bean11 == bean12){     
  13.             System.out.println("bean11 == bean12");     
  14.         }else{     
  15.             System.out.println("bean11 != bean12");     
  16.         }     
  17.     }     
  18. }    

【編輯推薦】

    Struts VS Spring兩種MVC框架比較

    Spring MVC框架的高級(jí)配置

    Spring MVC框架的高級(jí)配置(上)

    Spring MVC框架的高級(jí)配置(下)

    設(shè)計(jì)REST風(fēng)格的Java MVC框架:WebWind


網(wǎng)站名稱(chēng):詳解Spring中bean的作用域
轉(zhuǎn)載來(lái)于:http://www.dlmjj.cn/article/dhsjgdh.html