为了账号安全,请及时绑定邮箱和手机立即绑定

关于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 关注
  • 297 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信