Prototype Bean doesn't get autowired as expected

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

TestController.java

@RestController
public class TestController {

    @Autowired
    private TestClass testClass;

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


        
5条回答
  •  执念已碎
    2021-02-05 04:41

    You cannot autowire prototype bean (well, you can but the bean will be always the same)... autowire the ApplicationContext and get an instance of the needed prototype bean manually (for example in the constructor):

        TestClass test = (TestClass) context.getBean("nameOfTestClassBeanInConfiguration");
    

    In this way you're sure of getting a new instance of TestClass.

提交回复
热议问题