0
点赞
收藏
分享

微信扫一扫

Spring Bean作用域为多例情况下的注入问题

云岭逸人 2022-12-22 阅读 32


测试代码:

@RestController
@Scope("prototype")
public class TestController {

private static TestService testService;

//这个方法不要为static
@Autowired
private void setTestService(TestService testService){
System.out.println("注入了-----------");
TestController.testService = testService;
}

@RequestMapping("/r")
public Object test2(){
testService.doSth();
return null;
}}

运行结果:

Spring Bean作用域为多例情况下的注入问题_System

不管这个TestService是不是静态的都会多次注入。

举报

相关推荐

0 条评论