Persisting WAR Database Configuration?

两盒软妹~` 提交于 2020-04-16 02:26:47

问题


I am developing a Spring Boot (with Hibernate and React front-end) that is deployed as a single-WAR file. During the first-usage, the administrator will use the web UI to configure the back-end database connection and setup an Administrator user.

Since I won't be able to persist this to the database, I want to persist this to a file. Would using Hibernate H2's database to write this to a file for read on startup be a legitimate way to go?

I wanted to prevent having them configure it via a Java property, however, unless I default the file to the user's home-directory, I will need to have them configure the file location as a Java property.

But wanted to check if this is a reasonable approach for a Production application to be sold to customers. The only thing that will be stored in it are two tables with a couple rows of data total.


回答1:


Java Naming and Directory Interface (JNDI)

You should not be hard-coding runtime configuration details into your web app codebase. Instead, externalize that information. So when the database password changes, you need not re-compile your web app. When your sysadmin changes from Tomcat to Payara as your web container, for example, you need not re-compile your web app.

Externalizing this information in commonplace in enterprise apps. Often this configuration info is placed in a directory service such as an LDAP service.

Some web containers come with a directory service built-in. Study up on the details of your particular container and its features.

Java provides an API as a middle-man between your code and all the various directory service technologies: Java Naming and Directory Interface (JNDI). At deployment time, the sysadmin configures the directory service of your choice, and places some config files for your web container.

Your web app’s Java code uses JNDI to blindly request the needed service or object. Your web app does not know or care if the web container is Apache Tomcat using its own built-in directory service, or if your sysadmin is using a separate LDAP server to store deployment details.

For example, to connect to a database, your Java code would use the JNDI API to request a DataSource object from your naming/directory service. That object contains the needed database address, username, password, and various database options. Your Java code simply calls DataSource::getConnection on whatever DataSource implementation object is returned. This isolates your Java code from the details of the database configuration. The sysadmin and DBA are free to relocate or reconfigure the database server without breaking your web app.



来源:https://stackoverflow.com/questions/60471531/persisting-war-database-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!