我有一个像这样定义的 java spring 配置,
@Configuration
public class FirstConfiguration {
@Bean
FirstController firstController() {
return new FirstController(firstService());
}
@Bean
FirstService firstService() {
return new FirstServiceImpl(secondService());
}
}
现在这个配置中的 bean 依赖于这样定义的 SecondConfiguration,
@Configuration
public class SecondConfiguration {
@Bean
SecondController SecondController() {
return new SecondController(SecondService());
}
@Bean
SecondService secondService() {
return new SecondServiceImpl();
}
}
如何在 FirstConfiguration 中使用 secondService() bean?
最佳答案
由于 SecondService
是一个 bean,您可以将它注入(inject)到 firstService
方法中以配置另一个 bean:
@Bean
FirstService firstService(@Autowired SecondService secondService) {
return new FirstServiceImpl(secondService);
}
关于java - 如何使用多个spring配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44515306/