日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

bean作用域 設(shè)置創(chuàng)建bean是單實(shí)例還是多實(shí)例

作者:仰望星空的快樂 更新時(shí)間: 2022-05-10 編程語(yǔ)言

單實(shí)例指的是只生成一個(gè)實(shí)例對(duì)象。

package com.testdemo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main( String [] args)
    {
        ApplicationContext applicationContext =new ClassPathXmlApplicationContext("demo1.xml");
        Lession lession= applicationContext.getBean("lession",Lession.class);
        System.out.println(lession);
        Lession lession2= applicationContext.getBean("lession",Lession.class);
        System.out.println(lession2);
    }
}

?地址是一樣的,代表生成了一個(gè)lession對(duì)象,即使我聲明兩個(gè)lession,但是xml文件實(shí)際上只生成了一個(gè)lession對(duì)象(地址值都相同了,肯定是一個(gè))

多實(shí)例指的是 生成多個(gè)對(duì)象。??

修改 xml文件 增加 scope=“”prototype“”? ?

prototype 和 singleton的區(qū)別

1.prototype? 表示多實(shí)例? singleton? 表示單實(shí)例 (默認(rèn)值)

2.設(shè)置scope值是singleton的時(shí)候,加載spring文件時(shí)就會(huì)創(chuàng)建單實(shí)例對(duì)象

3.設(shè)置scope值是prototype的時(shí)候,不是在加載spring文件時(shí)創(chuàng)建對(duì)象,而是在調(diào)用getbean方法的時(shí)候創(chuàng)建更多的實(shí)例對(duì)象


  

?其他不變? ? ?兩個(gè)的地址值是不一樣的,說(shuō)明生成了兩個(gè)對(duì)象

?

bean作用域:

1.在spring中,設(shè)置創(chuàng)建bean是單實(shí)例還是多實(shí)例

2.在spring中,默認(rèn)bean是單實(shí)例對(duì)象

原文鏈接:https://blog.csdn.net/sharesb/article/details/123453525

欄目分類
最近更新