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

JBoss EAP6/AS7/WildFly: How to Use Properties Files Outside Your Archive--reference的更多相关文章

  1. jboss eap6出现Tags_$$_javassist_26 cannot be cast to javassist.util.proxy.ProxyObject的解决办法

    使用了spring,hibernate.部署在jboss eap6中时,查询时出现java.lang.ClassCastException: com.vteba.product.base.model. ...

  2. 转:Red Hat JBoss团队发布WildFly 8,全面支持Java EE 7并包含全新的嵌入式Web服务器

    原文来自于:http://www.infoq.com/cn/news/2014/02/wildfly8-launch Red Hat的JBoss部门今天宣布WildFly 8正式发布.其前身是JBos ...

  3. JBOSS EAP6.2.0的下载安装、环境变量配置以及部署

    JBOSS EAP6.2.0的下载安装.环境变量配置以及部署 JBoss是纯Java的EJB(企业JavaBean)server. 第一步:下载安装 1.进入官网http://www.jboss.or ...

  4. JBoss、Tomcat、JBoss EAP、JBoss AS、wildfly,JBoss EAP安装部署,JBoss各个版本下载,JBoss允许远程访问

    感谢: https://www.cnblogs.com/invlong/p/5983334.html https://blog.csdn.net/mooncarp/article/details/78 ...

  5. JBOSS EAP6 系列二 客户端访问位于EAR中的EJB时,jndi name要遵守的规则

    EJB 的 jndi语法(在整个调用远程ejb的过程中语法的遵循是相当重要的) 参见jboss-as-quickstarts-7.1.1.CR2\ejb-remote\client\src\main\ ...

  6. jboss eap6.1(1)

    最近决定把公司的项目从jboss3.x迁移出来,先试着摸索一下最新的jboss服务器,从jboss官网上下了一份jboss-eap-6.1,然后找资料准备学习,同时将此次迁移过程记录下来,以备后续复习 ...

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

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

  8. jboss eap6.1(5)(ejb升级)

    以前的项目是基于ejb2.x做的,ejb的配置文件为ejb-jar.xml和jboss.xml,现在把这个项目移到新版本服务器中的时候,报解析ejb-jar错误. 查阅许多资料才找到解决办法,原来jb ...

  9. jboss相关的术语

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

随机推荐

  1. Eclipse启动Tomcat报错,系统缺少本地apr库

    Eclipse启动Tomcat报错,系统缺少本地apr库. Tomcat中service.xml中的设置情况. 默认情况是HTTP协议的值:protocol="HTTP/1.1" ...

  2. php 之 查询 投票练习(0508)

    练习题目: 解题: 方法一: 1. 投票主页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  3. MySql数据库4【命令行赋权操作】

    MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant selec ...

  4. nginx——rewrite模块

    1.什么是Nginx的Rewrite规则? Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用PCRE(Perl Compatible Regular Expressio ...

  5. 如何让同一个字段在不同的view中显示不同的内容

    many2one 字段默认显示 对象的name字段, 但也可以定义name_get方法显示不同的内容 如res.partner 对象可以根据 context 内容是否显示 客户的地址,职位,email ...

  6. 显示Mac电脑下的隐藏文件

    1. 在应用程序里打开终端, cd 你的文件夹名 ls -a 即可显示该文件夹下的所有隐藏文件 2. 如果你想打开整个系统的隐藏文件可以在终端下输入以下命令 defaults write com.ap ...

  7. 深入浅出C语言中的堆和栈

    在谈堆栈的时候,我在这有必要把计算机的内存结构给大家简单的介绍下(高手们可以直接飘过) 一. 内存结构   每个程序一启动都有一个大小为4GB的内存,这个内存叫虚拟内存,是概念上的,真正能用到的,只是 ...

  8. Unity问答——请问一下动画状态机怎么判断动画是否播完了?

    这篇博客源自我在泰课在线的回答.链接:http://www.taikr.com/group/1/thread/233 问:请问一下动画状态机怎么判断动画是否播完了? 答: 1. 脚本参考 Animat ...

  9. 应用Oracle(Linux中的安装)

    Linux中安装Oracle,不同于windows. Linux在安装时,要作些必要的分区配置,以便进行Oracle的安装: 同时需要创建专门的数据库用户和组,并配置环境变量. root登录 使用 r ...

  10. Node.js回调概念

    什么是回调? 回调是一个异步等效的功能.在完成特定任务回调函数被调用. Node大量使用了回调.Node的所有的API都支持回调这样的一种方式. 例如,一个函数读取一个文件可能开始读取文件,并使得下一 ...