1. 程式人生 > >Spring Bean作用域為多例情況下的注入問題

Spring Bean作用域為多例情況下的注入問題

測試程式碼:

@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;
    }}

執行結果:

不管這個TestService是不是靜態的都會多次注入。