Most of you probably heard about Eclipse Microprofile, including Microprofile Config. A quick try from a desktop application that uses JavaFX shows that it works without any additional configuration: just add a implementation dependency to your classpath and you can use microprofile config. See below a simple example.
This results in:
Microprofile config works with JavaFX and this is great news. Microprofile is evolving quickly and JavaFX being able to work with it will give Java developers the ability to easily integrate desktop applications with java microservices.
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
package org.fxapps.microprofile.config; | |
import org.eclipse.microprofile.config.Config; | |
import org.eclipse.microprofile.config.ConfigProvider; | |
import javafx.application.Application; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
import javafx.util.Pair; | |
public class App extends Application { | |
public static void main(String[] args) { | |
launch(); | |
} | |
@Override | |
public void start(Stage stage) throws Exception { | |
final Config config = ConfigProvider.getConfig(); | |
TableView<Pair<String, String>> tableView = new TableView<>(); | |
Label lblProperty = new Label(); | |
TableColumn<Pair<String, String>, String> cKey = new TableColumn<>("Name"); | |
TableColumn<Pair<String, String>, String> cValue = new TableColumn<>("Value"); | |
cKey.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getKey())); | |
cValue.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().getValue())); | |
cValue.setPrefWidth(500); | |
tableView.getColumns().add(cKey); | |
tableView.getColumns().add(cValue); | |
config.getPropertyNames().forEach(p -> { | |
String value = config.getValue(p, String.class); | |
tableView.getItems().add(new Pair<>(p, value)); | |
}); | |
String myCustomProperty = config.getValue("my.custom.property",String.class); | |
lblProperty.setText("Value for my.custom.property: " + myCustomProperty); | |
stage.setTitle("Default Properties"); | |
stage.setScene(new Scene(new VBox(10, tableView, lblProperty), 800, 400)); | |
stage.show(); | |
} | |
} |
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.fxapps.microprofile.config</groupId> | |
<artifactId>microprofile-config-fx</artifactId> | |
<version>1.0</version> | |
<properties> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.wildfly.swarm</groupId> | |
<artifactId>microprofile-config</artifactId> | |
<version>2017.12.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
Comentários
Postar um comentário