How to load external property file in JBoss 7 classpath?

This page talks about how to bring application specific or external property file (any kind of file) into JBoss AS 7.x in the application classpath. Whenever we deploy our application into the application server like JBoss, it is good practice to seperate property files, configuration related files, etc out of deployment bundle. This way we can make sure that any configuration changes does not requires new build. We can edit the configurations in the external files and restart the application. Here are the steps to put external files into the JBoss AS 7.0 and above application classpath:

1) Create a new module for your application configuration files in the JBoss

Create folder structure as shown below and place a file called module.xml inside the last folder.

jboss-as-7-home-folder/modules/com/your-company/configuration/main/module.xml

Update your module.xml file with below shown content:

?
1
2
3
4
5
6
<?xml version= "1.0"  encoding= "UTF-8" ?> 
<module xmlns= "urn:jboss:module:1.1"  name= "com.your-company.configuration"
     <resources> 
         <resource-root path= "." /> 
     </resources> 
</module>

2) Add all property files under main folder

You have already created folder structure, move all your property files under main folder, these files should be placed along with module.xml file.

3) Create jboss-deployment-structure.xml file in your application and map your module in the xml

Create jboss-deployment-structure.xml file under your application WEB-INF directory of your WAR file. Update your jboss-deployment-structure.xml file with the given below content:

?
1
2
3
4
5
6
7
8
<?xml version= "1.0"  encoding= "UTF-8" ?> 
<jboss-deployment-structure> 
   <deployment> 
     <dependencies> 
       <module name= "com.your-company.configuration" /> 
     </dependencies> 
   </deployment> 
</jboss-deployment-structure>

4) Load a properties file from the classloader in your application

?
1
InputStream is =  this .getClass().getClassLoader().getResourceAsStream( "MyAppProp.properties" );
 

猜你喜欢

转载自blog.csdn.net/myicer/article/details/80077365
今日推荐