Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Am I right that method loadView will be called only once (when creating 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();
    }

   ...
}
like image 515
sinedsem Avatar asked Apr 07 '26 12:04

sinedsem


1 Answers

All spring beans are singleton by default. So if you are not in a @Configuration simply your answer is YES.

CAUTION: In your situation if you call getMainView more than once while creating other beans which happens in @Configuration, it will be called multiple times, but just while creating.

Furthermore, I recommend you to read this question.

like image 145
Sercan Ozdemir Avatar answered Apr 09 '26 01:04

Sercan Ozdemir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!