tags:

views:

2760

answers:

4

I have several J2EE web applications running inside the same application server and I wanted to externalize the properties files that are inside the jars to a new directory to make these properties visible.

What is the recommended way to load these properties files?

Thanks.

+5  A: 

Personally, I use the java.util.Properties Class and use the load() method to loadproperties.

InputStream inputStream  = MyProperties.class.getClassLoader().getResourceAsStream("file.properties");
this.properties = new Properties();
this.properties.load(inputStream);

Then I use the get() method to read properties as Strings.

I your properties files are outside your jar file, here are some useful tips to read it

http://lijinjoseji.wordpress.com/2007/09/14/reading-properties-file-from-outside-of-a-jar-file/

Tom
+1 The Properties class also has other cool methods like hasProperty()
Peter D
As my properties files are outside my jar file the best solution is the described in the link:1. If the resource file is located in your CLASSPATH, then objRes = ResourceBundle.getBundle("/ResFile.Application");Thanks.
David García González
+1  A: 

If you're using Spring, it's nice to abstract away in the code that there's even a properties file at all and use a PropertyPlaceHolderConfigurer. This allows you to externalize the properties from a BeanFactory definition to a standard Java properties file.

Example (ripped from the spring docs):

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:com/foo/jdbc.properties</value>
    </property>
</bean>

<bean id="dataSource" destroy-method="close"
      class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
Paul Morie
Spring is a very powerfull framework but the web applications deployed are not developed with this framework. I will take into account this for the next web applications. Thanks.
David García González
A: 

If you're using JBoss you can create a properties service file (named something-service.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server>

<server>    
  <mbean code="org.jboss.varia.property.SystemPropertiesService" 
           name="jboss:type=Service,name=MySystemProperties">        
    <attribute name="Properties">
       property_a=value a
       property_b=value b
    </attribute>         
  </mbean>
</server>

Copy this to your deploy directory and the properties will be available with System.getProperty()

mtpettyp
A simple solution but if you are using only JBoss, my application server is Weblogic.Thanks.
David García González
A: 

This link has some advice. You can either use ResourceBundle or do something like this:

// Assuming you are in a Servlet extending HttpServlet 
// This will look for a file called "/more/cowbell.properties" relative
// to your servlet Root Context
InputStream is = getServletContext().getResourceAsStream("/more/cowbell.properties");
Properties  p  = new Properties();
p.load(is);
is.close();

The recommended way for me will be to go with Spring, as another answer suggested.

kgiannakakis
But this solution only works if you have the properties file deployed inside the web application. If we have the properties files in another folder outside the web application I have to use other strategy.Thanks.
David García González