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 ~
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.
Let's inject beans into a JavaFX program like this.
First, let's summarize the functions for operating injection. This is plagiarism on the net.
SpringInjector.java
@Scope(BeanDefinition.SCOPE_SINGLETON)
@Component
public class SpringInjector {
@Autowired
private AutowiredAnnotationBeanPostProcessor autowiredProcessor;
@Resource
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
autowiredProcessor.processInjection(unmanagedBean);
}
}
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!
application-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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"/>
</beans>
Register the package annotation as a component. Register the springInjector as a bean.
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.
AbstractWebAquaStage.java
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);
si.inject(this);
}
public AbstractWebAquaStage() {
this.init();
//Output to the console for operation check
System.out.println("An instance has been created as the parent stage.");
}
}
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
TopStage.java
public class TopStage extends AbstractWebAquaStage {
@Autowired
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) {
e.printStackTrace();
}
Scene x_scene;
x_scene = new Scene(MainPane);
this.setScene(x_scene);
}
}
You should move this Stage to see it
AppMain.java
public class AppMain extends Application {
//Spring application Context(static)
public static ApplicationContext applicationContext;
@Override
public void start(Stage stage) {
stage = new TopStage();
stage.show();
}
public static void main(String args[]) {
launch();
}
}
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