JavaEE中的配置管理(Configuration Management in JavaEE)

编程入门 行业动态 更新时间:2024-10-24 04:52:22
JavaEE中的配置管理(Configuration Management in JavaEE)

我的目标

我有一个JavaEE环境(在我的特殊情况下,它是一个Glassfish Web Profile),我希望独立于容器的方式来配置具有以下功能的应用程序:

未指定其他内容时的默认配置(WAR文件内) 自定义配置(WAR文件外部)分为两层: 主机特定设置(在外部属性文件中;例如某个工作目录) 应用程序特定设置(在数据库中;例如邮箱大小)

我的愿望是,尽可能少的先决条件(现在唯一一个是JNDI数据源)来部署和运行我的应用程序(设置JNDI数据源,部署WAR文件,在某个配置文件夹中有一个可选的.properties文件,完成)。

这引出我的第一个问题:这是一个常见/好的/有用的设置,还是它不必要的复杂和/或非常奇特?

我的想法(到目前为止)

默认配置

默认配置将位于属性文件中:

src/main/resources/config/default.properties

应用程序作用域bean在初始化时读取此属性,如下所述:

@Named @ApplicationScoped public class Configuration implements Serializable { ... @PostConstruct public void initConfiguration() { loadDefaultConfiguration(); } private void loadDefaultConfiguration() { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try (InputStream input = classLoader.getResourceAsStream("/config/default.properties")) { properties.load(input); } catch(IOException ex) { LOGGER.error(...); } } }

我的环境

JavaSE 7 JavaEE 6 Glassfish 3.1.2 Web(但这不应该);))

更新

我在Glassfish的管理区域找到了一个地方,您可以指定一些易于访问的系统属性:

System.getProperty("myApp.propertyName");

My Goal

I’ve got a JavaEE environment (in my particular case it’s a Glassfish Web Profile) and I want a container independent way of configuring my application with the following features:

Default configuration when nothing else is specified (inside WAR file) Custom configuration (outside WAR file) in two layers: Host specific settings (in an external properties file; e.g. some working directory) Application specific settings (in database; e.g. mailbox size)

My wish would be that there are as few preconditions as possible (the only one now is a JNDI datasource) to deploy and run my application (Set up JNDI datasource, deploy WAR file, have an optional .properties file in some configuration folder and - done).

This leads me to my first question: Is this a common/good/useful setup or is it unnecessarily complicated and/or very exotic?

My Idea (so far)

Default Configuration

The default configuration would be in a properties file:

src/main/resources/config/default.properties

An application scoped bean reads this properties on initialization as described here:

@Named @ApplicationScoped public class Configuration implements Serializable { ... @PostConstruct public void initConfiguration() { loadDefaultConfiguration(); } private void loadDefaultConfiguration() { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try (InputStream input = classLoader.getResourceAsStream("/config/default.properties")) { properties.load(input); } catch(IOException ex) { LOGGER.error(...); } } }

My Environment

JavaSE 7 JavaEE 6 Glassfish 3.1.2 Web (but this should not matter ;) )

Update

I've found a place in the admin area of Glassfish where you can specify some system properties which are easily accessable:

System.getProperty("myApp.propertyName");

This could be used to store the path to the external .properties file, but I'm not sure if this is a clean way because

I don't know if every container (which supports JavaEE) has such a nice feature I don't really want to have plain file access from a web application

最满意答案

在与我的同事和一些研究交流之后,我已经为主机特定(外部)配置实施了以下内容。

由于我无论如何都需要一个工作目录,所以我决定使用这个工作目录作为我的外部配置的位置。 因此,我使用环境变量(例如MYAPP_HOME ),或者如果未设置该变量,则使用用户的主文件夹(例如<user.home>/.myapp ):

private Path discoverRootDirectory() { String myAppHome = System.getenv("MYAPP_HOME"); if (myAppHome == null) { return Paths.get(System.getProperty("user.home"), ".myapp"); } else { return Paths.get(myAppHome); } }

属性文件将像往常一样加载:

private void loadConfiguration() { properties = new Properties(); // ... try (InputStream inputStream = Files.newInputStream(discoverRootDirectory())) { properties.load(inputStream); } catch (FileAccessException | IOException ex) { // ... } }

After talking with my colleagues and some research I've implemented the following for the host specific (external) configuration.

As I need a working directory for my application anyway, I decided to use this working directory also as the location for my external configuration. Therefore I either use an environment variable (e.g. MYAPP_HOME) or, if the variable is not set, the user's home folder (e.g. <user.home>/.myapp):

private Path discoverRootDirectory() { String myAppHome = System.getenv("MYAPP_HOME"); if (myAppHome == null) { return Paths.get(System.getProperty("user.home"), ".myapp"); } else { return Paths.get(myAppHome); } }

The properties file will then be loaded as usual:

private void loadConfiguration() { properties = new Properties(); // ... try (InputStream inputStream = Files.newInputStream(discoverRootDirectory())) { properties.load(inputStream); } catch (FileAccessException | IOException ex) { // ... } }

更多推荐

本文发布于:2023-08-06 16:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:配置管理   JavaEE   Management   Configuration

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!