I read few spring tutorials with basic examples, and I am a bit confused on how to wire up things properly.
The trouble is, I wanted to use application context to pull singleton controller references, but I read on few other topics that application context should not be accessed directly unless it is absolutely necessary. I think I should use constructor to instantiate reference I want, but here things get all blurry for me.
I have javafx application with several fxml files. I have one main fxml and other are loaded dynamically inside main.
I'll use simplified code for example with two fxml controllers, MainController.java (for main fxml) and ContentController.java (for content fxml)
The idea is that content fxml has TabPane, and main fxml has button which opens new tab in TabPane on ContentController.
I am currently doing something like this
bean xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="contentController"
class="ContentController"
scope="singleton" />
</beans>
MainControler:
public class MainOverlayControler {
ApplicationContext context;
@FXML
private BorderPane borderPane;
@FXML
private void initialize() {
loadContentHolder();
}
@FXML
private Button btn;
@FXML
private void btnOnAction(ActionEvent evt) {
((ContentController)context.getBean("contentController")).openNewContent();
}
private void loadContentHolder() {
//set app context
context = new ClassPathXmlApplicationContext("Beans.xml");
Node fxmlNode;
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setController(context.getBean("contentController"));
try {
fxmlNode = (Node)fxmlLoader.load(getClass().getResource("Content.fxml").openStream());
borderPane.setCenter(fxmlNode);
} catch (IOException e) {
e.printStackTrace();
}
}
ContentController:
public class ContentController {
@FXML
private TabPane tabPane;
public void openNewContent() {
Tab newContentTab = new Tab();
newContentTab.setText("NewTab");
tabPane.getTabs().add(newContentTab);
}
}
Main class:
public class MainFX extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
Parent root = (Parent) fxmlLoader.load(getClass().getResource("main.fxml").openStream());
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
public static void main(String[] args) {
launch(args);
}
}
Question: I would like to know how to do same thing with constructor DI, or some other way if I misunderstood the concept.
I need to be able to call "openNewContent" on singleton instance of "ContentController" from multiple other controllers as well.