Created
September 15, 2018 18:56
-
-
Save YSRKEN/1cf1c458047e7d091f6e642e2107e94b to your computer and use it in GitHub Desktop.
Spring FrameworkでJavaFXプログラミングを楽にしたかった ref: https://qiita.com/YSRKEN/items/b7351b0ff15c76cb850e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
//@Scope(BeanDefinition.SCOPE_PROTOTYPE) | |
public class Controller{ | |
/** | |
* Model | |
*/ | |
@Autowired | |
Model model; | |
/** | |
* 初期化 | |
*/ | |
public void initialize() { | |
~~~ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 起動用クラス | |
* @author ysrken | |
*/ | |
@ComponentScan | |
public class MainApp extends Application { | |
private static ConfigurableApplicationContext context; | |
/** | |
* main関数 | |
* @param args コマンドライン引数 | |
* @throws Exception 実行時例外 | |
*/ | |
public static void main(String[] args) throws Exception { | |
// このcontextは今後使い回すので、あえて上記のようにstatic変数にした | |
context = new AnnotationConfigApplicationContext(MainApp.class); | |
launch(args); | |
} | |
/** | |
* JavaFXの起動処理 | |
* @param stage Stage情報 | |
* @throws Exception 実行時例外 | |
*/ | |
public void start(Stage stage) throws Exception { | |
// 無難に生成するが、getBeanしているのがポイント | |
FXMLLoader loader = new FXMLLoader(); | |
loader.setControllerFactory(MainApp.getApplicationContext()::getBean); | |
Parent rootNode = (Parent) loader.load(getClass().getResourceAsStream("/fxml/MainView.fxml")); | |
Scene scene = new Scene(rootNode); | |
stage.setScene(scene); | |
mainStage.show(); | |
} | |
/** | |
* ApplicationContextを引き回すために使用 | |
* @return 共用するApplicationContext | |
*/ | |
public static ApplicationContext getApplicationContext() { | |
return context; | |
} | |
/** | |
* JavaFXの終了処理 | |
* @throws Exception 実行時例外 | |
*/ | |
@Override | |
public void stop() throws Exception { | |
context.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
//@Scope(BeanDefinition.SCOPE_PROTOTYPE) | |
public class Model{ | |
/** | |
* 初期化 | |
*/ | |
publicModel() { | |
~~~ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class SampleClass { | |
/** | |
* Model | |
*/ | |
@Autowired | |
Hoge hoge; | |
~~~~~~ | |
} | |
// OK | |
final SampleClass x = MainApp.getApplicationContext().getBean(SampleClass.class); | |
// NG | |
final SampleClass y = new SampleClass(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment