I would like to add a file or class to my JavaFX project that only contains the configuration data of the project, e.g. the access data for the database, system paths etc. How would you do this? Just write everything in a normal class? There is definitely a better way, right?
Asked
Active
Viewed 473 times
0
-
1Resources, properties, preferences, databases, et al.; your requirements may help choose among design alternative. – trashgod Dec 31 '21 at 20:08
-
1Use a property file. See https://stackoverflow.com/questions/8285595/reading-properties-file-in-java/13667185 – swpalmer Dec 31 '21 at 20:20
-
Thanks guys, I'll take a look at the properties thing. – 68Doom68 Dec 31 '21 at 20:39
-
1If you are going to use a database and config properties, then I recommend [integrating JavaFX and SpringBoot](https://stackoverflow.com/questions/57887944/adding-spring-dependency-injection-in-javafx-jpa-repo-service). There are many tutorials at spring.io and baeldung which explain how to use spring data and configuration, for example [properties and Spring](https://www.baeldung.com/properties-with-spring) and [spring dara](https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa). Spring is not best for everyone, so measure against your requirements. – jewelsea Jan 01 '22 at 02:00
-
1Hi, I also think Spring is the best choice. My project is only very small and serves as an illustration for the bosses. Later, when I can continue to work on it, I will certainly go the way with Spring. But I still have to learn a little, as I have only programmed as a hobby so far. Incidentally, that with the properties worked great and it's already built in. Thanks again for the good tip. – 68Doom68 Jan 01 '22 at 13:12
1 Answers
2
You're right, of course I'll be happy to do that. First I created a property file in the project folder and call it app.properties:
db_url=jdbc:mysql://localhost:3306/db name
db_user=user name
db_pwd=secret password
instructions_folder=/home/username/documents/
Then I created a class that loads the properties and makes them available throughout the project.
public class AppProperties {
// FILENAME = Path to properties-file
// Store and protect it where ever you want
private final String FILENAME = "app.properties";
private static final AppProperties config_file = new AppProperties();
private Properties prop = new Properties();
private String msg = "";
private AppProperties(){
InputStream input = null;
try{
input = new FileInputStream(FILENAME);
// Load a properties
prop.load(input);
}catch(IOException ex){
msg = "Can't find/open property file";
ex.printStackTrace();
}finally{
if (input != null){
try{
input.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
public String getProperty (String key){
return prop.getProperty(key);
}
public String getMsg () {
return msg;
}
// == Singleton design pattern == //
// Where ever you call this methode in application
// you always get the same and only instance (config_file)
public static AppProperties getInstance(){
return config_file;
}
}
In the DBUtilitis class, where I do my database queries, I now load the properties into final variables and use them in the query methods.
private static final String db_url = AppProperties.getInstance().getProperty("db_url");
private static final String db_user = AppProperties.getInstance().getProperty("db_user");
private static final String db_pwd = AppProperties.getInstance().getProperty("db_pwd");
If I have not completely misunderstood this, the advantage of property files is that they can be stored and protected somewhere on the server. I hope the solution is not completely wrong - it works well anyway. I am always happy to receive suggestions and / or improvements.

68Doom68
- 29
- 7
-
1More on property security [here](https://security.stackexchange.com/search?tab=votes&q=password%20java%20properties%20file). – trashgod Jan 01 '22 at 19:20
-
-
1Solution for [encrypting properties](http://www.jasypt.org/encrypting-configuration.html) if needed. – jewelsea Jan 01 '22 at 20:51
-