关于bean的详细问题,请看下面具体情况

spring mvc 里@Autowired自动注入的bean(jdbcTemplate) 和ApplicationContext的getBean()方法获取的bean竟然不是同一个bean(hashCode不一样),请问怎么手动获取和@Autowired自动注入的一样的bean呀???

三国纷争
浏览 302回答 2
2回答

小怪兽爱吃肉

当然不一样。我今天在做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去拿否则这个注解没有任何意义

蓝山帝景

一个是启动是自动注入的,一个是获取的..感觉是这样..
打开App,查看更多内容
随时随地看视频慕课网APP