spring mvc 里@Autowired注入的bean和ApplicationContext的getBean获取的bean不是同一个(hashCode不一样)
springmvc里@Autowired自动注入的bean(jdbcTemplate)和ApplicationContext的getBean()方法获取的bean竟然不是...
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去拿否则这个注解没有任何意义
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去拿否则这个注解没有任何意义
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询