In Spring, can I autowire new beans from inside an autowired bean?

后端 未结 1 710
攒了一身酷
攒了一身酷 2021-01-02 23:51

I normally just @Autowire things into spring objects. But I\'ve encountered a situation where I need to dynamically create some objects which require values that could be au

相关标签:
1条回答
  • 2021-01-03 00:11

    You can use AutowireCapableBeanFactory:

    @Service 
    public class Foo { 
        @Autowired private AutowireCapableBeanFactory factory; 
    
        private <T> T createAutowiredObject(Class<T> c) {
            return factory.createBean(c);
        }
        ...
    }
    
    0 讨论(0)
提交回复
热议问题