我有这样定义的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,因此可以将其注入firstService方法以配置另一个bean:

@Bean
FirstService firstService(@Autowired SecondService secondService) {
    return new FirstServiceImpl(secondService);
}

09-26 10:04