I tried to link JavaFX and Spring Framework.

I wanted a DI container function for a binary application (´ ・ ω ・ `)

Well, what I wanted to do. @Autowired. If you use it, you don't have to use new, and the design is refreshing.

There is no such thing as FXML and a controller being connected like other people. I made it just by moving the DI container ~

That's why it's implemented

If it is a GUI application, it is necessary to explicitly load the Bean definition and run Spring. Then inject the bean into the running Java program.

  1. Run Spring
  2. Inject the bean.

Let's inject beans into a JavaFX program like this.

Create a class that injects into the instance.

First, let's summarize the functions for operating injection. This is plagiarism on the net.


public class SpringInjector {
    private AutowiredAnnotationBeanPostProcessor autowiredProcessor;
    private CommonAnnotationBeanPostProcessor commonProcessor;
    public void inject(final Object unmanagedBean) {
        // javax.*Injection of related annotations(@Resoure)
        commonProcessor.postProcessPropertyValues(null, null, unmanagedBean, unmanagedBean.getClass().getSimpleName());
        // @Autowire、@Value annotation injection

The ** AnnotationBeanPostProcessor ** that appears here is It seems to be a function that injects beans into a certain class. Wagwanny

Register this class as a bean!


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <!-- Uncomment and add your base-package here:-->

    <context:annotation-config />
     <context:component-scan base-package="com.TsugaruInfo.*"></context:component-scan>
    <!--Spring Injector registration-->
    <bean id="springInjector" class="com.Tsugaruweb.springUtils.SpringInjector"/>

Register the package annotation as a component. Register the springInjector as a bean.

Enable Spring on parent stage class

Call ApplicationContext in Spring in the parent class. (The feeling that you could call it from the console in the same way)

Realize Bean management here new ClassPathXmlApplicationContext("spring/application-config.xml"); Inject Spring Injector from DI container, Performs the Bean injection function of SprinInjector.


public class AbstractWebAquaStage extends Stage {

    //Spring application Context(static)
    public static ApplicationContext applicationContext;

	 *Enable Spring application context and@Inject Autowired
	public void init() {
		applicationContext = new ClassPathXmlApplicationContext("spring/application-config.xml");
		SpringInjector si = applicationContext.getBean(SpringInjector.class);
	 public AbstractWebAquaStage() {
	    //Output to the console for operation check
	    System.out.println("An instance has been created as the parent stage.");

Now that we have set it up, let's actually move it.

I was able to set it up with a good feeling. It's cool (committee to call the program cool) Now let's inherit this parent class Let's create a Stage with annotation injection function


public class TopStage extends AbstractWebAquaStage {
	ResourceLoader resource;
 *Top stage
	public TopStage(){
		//Root pane
		Pane MainPane = null;
		try {
			MainPane = (Pane)FXMLLoader.load(resource.getResource("classpath:fxml/Tekito.fxml").getURL());
		} catch (IOException e) {
		Scene x_scene;
		x_scene = new Scene(MainPane);


You should move this Stage to see it


public class AppMain extends Application {

    //Spring application Context(static)
    public static ApplicationContext applicationContext;
	public void start(Stage stage) {
		stage = new TopStage();
	public static void main(String args[]) {


Let's see if it works


I was able to confirm that it worked properly ヾ (.> ﹏ <.) ノ cool!

reference Add Star for Spring Bean injection in JavaFX Controller

Recommended Posts

I tried to link JavaFX and Spring Framework.
I tried to link grafana and postgres [docker-compose]
I tried to verify this and that of Spring @ Transactional
I wanted to make JavaFX programming easier with the Spring Framework
[I tried] Spring tutorial
I tried Spring Batch
I introduced OpenAPI (Swagger) to Spring Boot (gradle) and tried various settings
I tried to implement file upload with Spring MVC
I tried to read and output CSV with Outsystems
05. I tried to stub the source of Spring Boot
I started MySQL 5.7 with docker-compose and tried to connect
I tried to integrate AWS I oT button and Slack
I tried to reduce the capacity of Spring Boot
I tried to get started with Spring Data JPA
I tried to chew C # (reading and writing files)
I tried Spring State machine
I tried to verify yum-cron
I tried to create a shopping site administrator function / screen with Java and Spring
I tried to collect and solve Ruby's "class" related problems.
I tried to summarize the basics of kotlin and java
I tried to make Java Optional and guard clause coexist
I tried to link chat with Minecraft server with Discord API
I tried to summarize personally useful apps and development tools (Apps)
[Rails] I tried to implement "Like function" using rails and js
I tried to get started with Swagger using Spring Boot
I went to JJUG CCC 2019 Spring
I tried using Spring + Mybatis + DbUnit
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to explain the method
I tried the Java framework "Quarkus"
I tried GraphQL with Spring Boot
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried Flyway with Spring Boot
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried printing a form with Spring MVC and JasperReports 3/3 (Spring MVC control)
I tried to integrate Docker and Maven / Netbean nicely using Jib
I tried connecting to MySQL using JDBC Template with Spring MVC
I tried to create a Spring MVC development environment on Mac
How to set and use profile in annotation-based Configuration in Spring framework
I tried to see if Koalas and Elasticsearch can work together
I tried to summarize the methods of Java String and StringBuilder
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I introduced WSL2 + Ubuntu to Window10 and tried using GDC, DMD, LDC
I tried to summarize Java lambda expressions
I tried printing a form with Spring MVC and JasperReports 2/3 (form template creation)
Major changes related to Spring Framework 5.0 Test
I tried to get started with WebAssembly
I tried to summarize the key points of gRPC design and development
I tried to solve AOJ's Binary Search
I tried to make my own transfer guide using OpenTripPlanner and GTFS
I made a virtual currency arbitrage bot and tried to make money
I tried to implement the Iterator pattern
I tried Lazy Initialization with Spring Boot 2.2.0
I tried to summarize the Stream API
I tried Spring Data JDBC 1.0.0.BUILD-SNAPSHOT (-> 1.0.0.RELEASE)