spring mvc 里@Autowired注入的bean和ApplicationContext的getBean获取的bean不是同一个(hashCode不一样)

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

当然不一样。我今天在做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去拿否则这个注解没有任何意义
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-08-16
一个是启动是自动注入的,一个是获取的..感觉是这样..