In order to config my Spring application, I use an application.properties
file, and a configuration class like so:
@Configuration
@ComponentScan(basePackages = {"org.fg.soma.manager.*"})
@PropertySource(value = "file:config/application.properties")
public class SpringConfig {
@Autowired
public SpringFXMLLoader springFXMLLoader;
@Bean
@Lazy // Create only after spring context bootstrap
public StageManager stageManager(Stage stage) {
return new StageManager(springFXMLLoader, stage);
}
}
I configure here a bean for the class StageManager, which helps to switch scenes in my JavaFX application. This is a part of the actual class:
public final class StageManager {
private final SpringFXMLLoader springFXMLLoader;
private Stage stage;
public StageManager(SpringFXMLLoader springFXMLLoader, Stage stage) {
this.stage = stage;
this.springFXMLLoader = springFXMLLoader;
}
SpringFXMLLoader
's job is to load the FXML file via Spring and not via JavaFX so it could be later used as a bean:
@Component
public class SpringFXMLLoader {
private final ApplicationContext applicationContext;
@Autowired
public SpringFXMLLoader(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public Parent load(String fxmlPathName) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(applicationContext::getBean);
loader.setResources(ResourceBundle.getBundle("Bundle"));
loader.setLocation(getClass().getResource(fxmlPathName));
return loader.load();
}
}
Finally, I have the main Spring Boot application class which launches my JavaFX application:
@SpringBootApplication
public class ModManagerApplication extends Application {
private ConfigurableApplicationContext springContext;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void init() {
String[] args = getParameters().getRaw().toArray(new String[0]);
springContext = new SpringApplicationBuilder(ModManagerApplication.class)
.headless(false)
.run(args);
}
@Override
public void start(Stage primaryStage) throws IOException {
StageManager stageManager = springContext.getBean(StageManager.class, primaryStage);
stageManager.switchScene(ApplicationView.MOD_MANAGER);
}
@Override
public void stop() {
springContext.stop();
}
}
When I run my application, I get the following error in the console:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.fg.soma.manager.view.StageManager' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:352)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1133)
at org.fg.soma.manager.application.ModManagerApplication.start(ModManagerApplication.java:30)
Line 30 refers to this line: StageManager stageManager = springContext.getBean(StageManager.class, primaryStage);
I use Intellij-IDEA 2020 to configure my Spring Boot Facets like so:
I have no idea what could cause this, so any help is appreciated.