亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于bean的詳細問題,請看下面具體情況

關于bean的詳細問題,請看下面具體情況

三國紛爭 2021-06-16 15:11:15
spring mvc 里@Autowired自動注入的bean(jdbcTemplate) 和ApplicationContext的getBean()方法獲取的bean竟然不是同一個bean(hashCode不一樣),請問怎么手動獲取和@Autowired自動注入的一樣的bean呀???
查看完整描述

2 回答

?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

當然不一樣。我今天在做springbatch的 job時遇到了同樣的問題,到谷歌搜了一下:
A bean with scope prototype just means that everytimeApplicationContext.getBean is called a new instance of the bean is created. If you try to inject a prototype bean in a singleton, your prototype bean will be injected once (and so is no more a prototype).
大意就是說spring管理的bean默認是單例模式,這種情況下autowired和getbean拿到的沒區別,但是如果你加入了@Scope(SCOPE_PROTOTYPE)那么就必須用getbean去拿否則這個注解沒有任何意義

查看完整回答
反對 回復 2021-06-21
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

一個是啟動是自動注入的,一個是獲取的..感覺是這樣..

查看完整回答
反對 回復 2021-06-21
  • 2 回答
  • 0 關注
  • 360 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號