Getting acknowledged with "Spring boot + JavaFX" coop.
Was hoping to get answers for such questions:
I'm not that good with 'JavaFX' yet, and I've tried to start that bean by calling the 'Application.start(UIClass.class)' and got no spring functionality(I get the idea that this doesn't start the object bean, but initiates it separately).
In a similar thread (Spring Boot Main and JavaFX) I've found a solution to use the:
ApplicationContext ctx = SpringApplication.run(Root.class);
ctx.getAutowireCapableBeanFactory().autowireBean(this);
but I have the feeling that this is more of a workaround and I've made a mistake somewhere.
Thanks in advance.
In general, I'd make a case for arguing that if you are accessing the Application
instance from another part of the application, you probably need to refactor your application. The role of the Application
is just to manage application lifecycle, so accessing that object from elsewhere is a sign that you are likely violating design principles somewhere (single responsibility, at a minimum).
That said, there are some corner cases. I have one application where I needed to access the HostServices
in a controller, and that is only generally available via the Application
instance. In that case I used a similar solution/workaround to the one you propose in order to programmatically register the HostServices
as a "Spring-managed" bean.
The bottom line here is that you have two toolkits/frameworks that are responsible for creating and managing object lifecycle to some degree. (JavaFX is more of a toolkit than a framework, and doesn't do much object lifecycle management, but it does some.) Any time you are in that situation and you want one framework to be aware of an object created and managed by the other framework, you need to do some programmatic wiring between the two frameworks. This is one example, because the Application
instance is created for you by JavaFX, so if you want Spring to be aware of it, you have to explicitly tell Spring about it.
So in summary, properly structuring your application will minimize the need for the kinds of workaround you suggest, but there are cases where it might be necessary. There's generally no way to configure one framework to support objects created by another framework unless there is explicit support for the second framework in the first.