Prototype Bean doesn't get autowired as expected

后端 未结 5 895
南旧
南旧 2021-02-05 04:14

TestController.java

@RestController
public class TestController {

    @Autowired
    private TestClass testClass;

    @RequestMapping(value = \"/test\", method         


        
5条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 04:24

    Spring controllers are singletons by default (which is OK due to their stateless nature), as well as the other Spring beans.

    That's why it is enough to instantiate only one TestClass instance for the only TestController instance.

    It is easy to instantiate TestClass one more time - just inject it in another controller or get from the context programmatically

提交回复
热议问题