Introduction

I’d like to preface this post with, really, all properties files should be inside your application archive. However, there are occasions, where you do need properties files more easily accessible — where a modification doesn’t require breaking open the EAR/WAR, making the modification, then rebuilding or repackaging the EAR/WAR.

With previous versions of EAP and AS, the conf directory was on the class path and you could drop your properties files in conf/props and be happy.  EAP6 and AS7 (now WildFly) do not have the configuration directory on the classpath however, and it really should not be added.  So what is the solution? Modules!

These newer versions (at the time or writing this) of JBoss take an OSGi approach to class loading where everything is considered a module.  A module can import (require the dependency) or export (allow other to depend) resources.

You can write a simple module to make resources (such as properties files) available to an application that declares it as a dependency.  This is the approach that we will take

Writing the Module

Writing the module is pretty simple.  The first thing we will do is create our module directory structure in the modules directory. The module name should follow the standard Java style package naming.  Each name in the package (separated by full-stops ‘.’) should have its own directory.  See below for a module that will be named com.jyore.examples.settings:

1
2
3

EAP_HOME/modules/com/jyore/examples/settings

Now, there will be subdirectories under the settings directory, if you want to use different slots, but to keep it simple, we will use the default main slot.  So create a single subdirectory called main inside the settings directory.

1
2
3

EAP_HOME/modules/com/jyore/examples/settings/main

The module descriptor will be placed inside the main directory.  Add the following:

1
2
3
4
5
6
7
8

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.jyore.examples.settings">
    <resources>
        <resource-root path="."/>
    </resources>
</module>

This descriptor will put all of the resources under the main directory at the root of the class path for us when we declare the dependency.  This can be done one of two ways:

a) MANIFEST.MF Dependencies: entry

1
2
3

Dependencies: com.jyore.examples.settings

b) jboss-deployment-structure.xml entry

1
2
3
4
5
6
7
8
9
10

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="com.jyore.examples.settings"/>
        </dependencies>
    </deployments>
</jboss-deployment-structure>

If using the jboss-deployment-structure.xml method, this file is placed in the WEB-INF directory of a WAR or the META-INF directory of an EAR.

Testing the Module

No ‘How To’ is complete without a test case. So, let’s see how a simple web app can use this module to get properties.

This example will build a simple web app that will dump the contents of the properties files to a table in a web page.

Java Code – ModuleReader.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

package com.jyore.examples.settings;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class ModuleReader {
    private static final String PROPERTIES_FILENAME = "application_settings.properties"
    private static Properties settings = null;
    public static void loadSettings() {
        settings = new Properties();
        try {
            settings.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String getProperty(String key) {
        if(settings == null) {
            return null;
        } else {
            return settings.getProperty(key);
        }
    }
    public static Set<Object> getKeys() {
        if(settings == null) {
            return null;
        } else {
            return settings.keySet();
        }
    }
}

Now for the jsp’s

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

<%@page import="com.jyore.examples.settings.ModuleReader,java.util.set"%>
<html>
    <head>
        <title>Module Settings Reader</title>
        <style>
            table, th, td {
                border : solid 1px black;
            }
        </style>
    </head>
    <body>
        <h1>Module Settings Reader</h1>
        <button onclick="loadProps();">Click to load properties</button>
        <%
            Set<Object> keys = ModuleReader.getKeys();
            if(keys == null) {
                out.print("<span style=\"color:red\">properties file not loaded</span><br/>");
            } else {
                out.print("<span style=\"color:green\">properties loaded</span><br/><table><tr><td><b>Key</b></td><td><b>Value</b></td></tr>");
                String value;
                for(Object str : value) {
                    String key = (String) str;
                    value = ModuleReader.getProperty(key);
                    out.print("<tr><td>"+key+"</td><td>"+value+"</td></tr>");
                }
                out.print("</table>);
            }
        %>
        <script type="text/javascript">
            function loadProps() {
                window.location.href = "loadProps.jsp";
            }
        </script>
    </body>
</html>

loadProps.jsp

1
2
3
4
5
6
7

<%@page import="com.jyore.examples.settings.ModuleReader"%>
<%
    ModuleReader.loadSettings();
    response.sendRedirect("index.jsp");
%>

My WAR Structure (propertyReader.war)

1
2
3
4
5
6
7
8
9
10
11
12
13
14

propertyReader.war
|-- WEB-INF
|   |-- classes
|   |   `-- com
|   |       `-- jyore
|   |           `-- examples
|   |               `-- settings
|   |                   `-- ModuleReader.class
|   |-- web.xml
|   `-- jboss-deployment-structure.xml
|-- index.jsp
`-- loadProps.jsp

and the module

1
2
3
4
5
6
7
8
9
10

modules
`-- com
    `-- jyore
        `-- examples
            `-- settings
                `-- main
                    |-- module.xml
                    `-- application_settings.properties

Now we deploy the application and go to http://localhost:8080/propertyReader/

You will see in red text that the properties are not yet loaded, so click the button.  This will go to the loadProps page, load the properties file, and redirect back.  This time, the properties within application_settings.xml will be displayed within a table on the page.

Open that properties file and edit it by adding another value.  Click the button on the web page again and see that the table updates with your addition.

原文地址:http://blog.jyore.com/?p=58

转载于:https://www.cnblogs.com/davidwang456/p/3906972.html

JBoss EAP6/AS7/WildFly: How to Use Properties Files Outside Your Archive--reference相关推荐

  1. jboss4 迁移_应用程序服务器迁移:从JBoss EE5到Wildfly EE7

    jboss4 迁移 几周前,我发布了一个有关从Java EE 5迁移到7的博客 .这主要是关于如何使用新的Java EE 7改进Java EE 5代码. 现在,在这篇文章中,我将对应用程序服务器端的迁 ...

  2. 应用程序服务器迁移:从JBoss EE5到Wildfly EE7

    几周前,我发布了一个有关从Java EE 5迁移到7的博客 .这主要是关于如何使用新的Java EE 7改进Java EE 5代码. 现在,在本文中,我将对应用程序服务器端的迁移路径进行一些研究. 如 ...

  3. jboss eap6.1(4)(部署应用)

    1.添加应用war包 手动部署,添加war包到standalone\deployments下,手工创建一个文件,如war包名称是a.war,创建一个a.war.deployed文件,内容随意. 2.  ...

  4. jboss相关的术语

    1 jboss eap java ee application server.red hat官方版本. 2 jboss as/wildfly java ee application server的社区 ...

  5. Jboss/Wildfly安装配置

    Jboss/Wildfly安装配置 官方网站: http://wildfly.org/ http://www.jboss.org/products/eap/overview/ http://www.o ...

  6. JBOSS EAP实战(2)-集群、NGINX集成、队列与安全

    JBOSS HTTP的Thread Group概念 JBOSS是一个企业级的J2EE APP Container,因此它和任何一种成熟的企业级中间件一样具有Thread Group的概念. 所谓Thr ...

  7. jboss简单使用--刚开始接触,感觉还是比较详细的

    初学Jboss,对于Jboss的基础认识以及配置做一些记录 Jboss基础: JBoss是什么 –基于J2EE的应用服务器 –开放源代码 –JBoss核心服务不包括支持servlet/JSP的WEB容 ...

  8. JBOSS EAP实战(1)

    JBOSS的诞生 1998年,在硅谷SUN公司的SAP实验室,一个年轻人正坐在电脑前面思考,然后写着什么东西. 不,他没有在写程序,他在写辞呈.他正在做出人生的一个重大决定: 他要辞掉在SUN的这份工 ...

  9. wildfly 21的配置文件和资源管理

    文章目录 简介 wildfly的配置文件 extensions profile path interface socket-binding management 资源管理 总结 简介 在上一篇文章我们 ...

最新文章

  1. [转]FPGA的GTP信号PCB布线要点
  2. IE兼容问题IE6,IE7,IE8,IE9,IE10
  3. 百度地图的立体效果来实现
  4. 人脸识别翼闸使用规范_人行通道闸如何搭配人脸识别使用
  5. react如何监听路由url变化
  6. ubuntu 20.04双系统安装_win10上跑Ubuntu不用虚拟机不用双系统!
  7. android收货地址整理
  8. 萌新的Python练习实例100例(五)输入三个整数x,y,z,请把这三个数由小到大输出。
  9. 蓝桥杯 ALGO-116算法训练 最大的算式
  10. 蒙特卡洛树搜索_蒙特卡洛树搜索与Model-free DRL
  11. 2.3 Hightway Networks
  12. Ubuntu设置终端相对短路径
  13. 详解两个队列实现一个栈(python实现——经典面试题)
  14. 【asp.net core 系列】6 实战之 一个项目的完整结构
  15. 【强推】8个实用的Python程序
  16. 拭血长短句手札【2013-2017】微信公众号 shixuemp
  17. un1que成员介绍
  18. 新手理解光猫和路由器
  19. thinkphp update操作,某字段更新不成功
  20. zoj3598----球面三角形内角

热门文章

  1. python绘制一个圆_Python在网格上绘制一个填充的“圆”
  2. 出色性能服务器,浪潮服务器:演绎出色传输与存储性能
  3. 诚毅学院全国计算机考试,集美大学2017年9月全国计算机等级考试报名时间
  4. float取整数部分_一步一步学Python3(小学生也适用) 第六篇: 变量及整数(int)类型...
  5. linux安装oracle 操作系统内核参数 aio,Linux安装Oracle 11G过程(测试未写完)
  6. pandas 合并数据
  7. 建立能够持续请求的CS网络程序
  8. 继承关系中的拷贝构造函数和赋值操作重载函数分析
  9. mysql 日期查询_Mysql日期查询list
  10. 广州那所大学有自考计算机专业,广州自考本科大学有哪些