Spring 是否用 java 配置中的现有 bean 替换方法调用?

Does Spring replaces method calls with existing bean in java config?

我说的对吗 loadView 方法只会被调用一次(在创建 mainView bean 时)?

@Configuration
public class Config {

    @Bean(name = "mainView")
    public View getMainView() throws IOException {
        return loadView("fxml/main.fxml");
    }

    @Bean
    public MainController getMainController() throws IOException {
        return (MainController) getMainView().getController();
    }

    @Bean
    public Step1Controller getStep1Controller() throws IOException {
        return getMainController().getStep1Controller();
    }

   ...
}

默认情况下,所有 spring 个 bean 都是单例的。因此,如果您不在 @Configuration 中,您的答案是 YES.

注意: 在您的情况下,如果您在创建发生在 @Configuration 中的其他 bean 时多次调用 getMainView,它将被调用多次, 但就在创建时。

此外,我建议您阅读this question