博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 注解设置bean非单例
阅读量:3930 次
发布时间:2019-05-23

本文共 2310 字,大约阅读时间需要 7 分钟。

概述通过Spring管理的类,默认是单例模式,但是如果有的类需要使用独立的属性,则需要配置为多例模式的. 但是多例模式不仅仅只是加一个声明,使用@Autowired进行注入,可能并不会是你想要的结果.因为多例模式的类是需要单独调用的.不搞清楚原理直接测试:需要多例的类上加上注解@Scope(“prototype”)@Component@Scope("prototype")public class ExampleService{
public void test(){
System.out.println("test,current bean is" + this); } }引用直接使用@Autowired@Controllerpublic class ExampleService{
@Autowired private ExampleService exampleService; @RequestMapping("test") public void test(){
exampleService.test(); } }结果: 每个request过来的时候,exampleService实例均为同一个实例.解决办法:第一种:不使用@Autowired@Controllerpublic class ExampleService{
@Autowired private org.springframework.beans.factory.BeanFactory beanFactory; @RequestMapping("test") public void test(){
ExampleService exampleService = beanFactory.getBean(ExampleService.class); exampleService.test(); }}第二种:使用bean工厂@Autowired private ApplicationContext context; @Bean public WebSocketHandler websocketBHandler() {
PerConnectionWebSocketHandler perConnectionHandler = new PerConnectionWebSocketHandler(WebSocketBHandler.class); perConnectionHandler.setBeanFactory(context.getAutowireCapableBeanFactory()); //设置bean工厂,否则bean工厂WebSocketBHandler将不会自动连接 return perConnectionHandler; }然后使用ApplicationContext进行代理bean工厂注入@Autowiredprivate ApplicationContext context;使用this.Bservice = context.getBean(BService.class, this);官方文档说明4.5.3 Singleton beans with prototype-bean dependencies When you use singleton-scoped beans with dependencies on prototype beans, be aware that dependencies are resolved at instantiation time. Thus if you dependency-inject a prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated and then dependency-injected into the singleton bean. The prototype instance is the sole instance that is ever supplied to the singleton-scoped bean. However, suppose you want the singleton-scoped bean to acquire a new instance of the prototype-scoped bean repeatedly at runtime. You cannot dependency-inject a prototype-scoped bean into your singleton bean, because that injection occurs only once, when the Spring container is instantiating the singleton bean and resolving and injecting its dependencies. If you need a new instance of a prototype bean at runtime more than once, see Section 4.4.6, “Method injection”

转载地址:http://jetgn.baihongyu.com/

你可能感兴趣的文章
LeetCode刷题:1129. 颜色交替的最短路径(JAVA代码解题)
查看>>
LeetCode刷题:675. 为高尔夫比赛砍树(JAVA代码详解)
查看>>
[赢得面试] JAVA开发工程师面试题解(持续更新)
查看>>
研发主管的烦恼:没有Product Owner有效参与的Sprint迭代能交付可工作的软件吗?
查看>>
[敏捷开发培训] 什么是敏捷开发中的Spike?
查看>>
[敏捷开发培训] 精益软件开发中的8中浪费(Lean Software Development)
查看>>
[敏捷开发实践] 高质量软件交付之概念模型
查看>>
研发主管的烦恼:如何考核Project Manager
查看>>
杂谈数字化转型(Data Transformation,DX)
查看>>
JAVA算法:无向图的表示
查看>>
[数据分析学习笔记] 数据分析处理流程
查看>>
[数据分析学习笔记] 数据挖掘建模过程
查看>>
[数据分析学习笔记] 数据分析必备的概率论和统计学知识
查看>>
[数据分析学习笔记] 异常值分析方法
查看>>
[统计学笔记] 统计学学习笔记重点总结
查看>>
[统计学笔记] (一) 统计学的基本概念
查看>>
[统计学笔记](二)收集数据
查看>>
[统计学笔记三] 整理和显示数据
查看>>
[统计学笔记九] 方差分析(ANOVA)
查看>>
[机器学习笔记] (一)机器学习基本概念篇(含思维导图)
查看>>