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去拿否則這個注解沒有任何意義
添加回答
舉報
0/150
提交
取消