码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-12 13:16:14      阅读:475      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

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:

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.

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

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

b) jboss-deployment-structure.xml entry

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

Now for the jsp’s

index.jsp

loadProps.jsp

My WAR Structure (propertyReader.war)

and the module

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,布布扣,bubuko.com

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

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/davidwang456/p/3906972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!