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

后端 未结 1 721
攒了一身酷
攒了一身酷 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 createAutowiredObject(Class c) {
            return factory.createBean(c);
        }
        ...
    }
    

    0 讨论(0)
提交回复
热议问题