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

阅读 32

2022-12-22


测试代码:

@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)

0 0 举报