網(wǎng)站首頁(yè) 編程語(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
相關(guān)推薦
- 2022-09-21 Oracle數(shù)據(jù)庫(kù)對(duì)象的使用詳解_oracle
- 2022-03-19 關(guān)于docker中?WSL?配置與修改問(wèn)題_docker
- 2022-12-03 FFmpeg?Principle學(xué)習(xí)open_output_file打開輸出文件_Android
- 2022-07-21 React生命周期
- 2022-06-27 Abp集成HangFire開源.NET任務(wù)調(diào)度框架_實(shí)用技巧
- 2022-11-05 Kotlin方法與Lambda表達(dá)式實(shí)踐使用介紹_Android
- 2023-07-03 Docker之容器導(dǎo)出為鏡像問(wèn)題_docker
- 2022-07-03 TypeScript 變量聲明 —— 類型斷言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支