Sunday, June 22, 2008

Java Web Application Properties

One of the most common application requirements is usually to create a central place where to keep application properties or application settings. These properties are normally settings or general properties that are applied to the application during runtime. A number of options exist, including creating string properties on your application server and accessing them as JNDI resources. However one of the still most practical approaches is to keep a properties config file with a simple propertyName=propertyValue type of structure. The example below is an example of an Application Config class that loads the application properties from a file named config.properties which is stored in the root of the source packages (root package) and hence automatically deployed with the application jar file during the build process.

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

import java.util.logging.Level;

import java.util.logging.Logger;

public class ApplicationConfig {

    private static Properties properties;

    private static ApplicationConfig config;

    static {

        config = new ApplicationConfig();

    }

    private ApplicationConfig ()  {

        try {

            InputStream in =           this.getClass().getClassLoader().getResourceAsStream("config.properties");

            properties = new Properties();

            properties.load(in);

        } catch (IOException ex) {

            Logger.getLogger(ApplicationConfig.class.getName()).log(Level.SEVERE, null, ex);

        }

    }

    public static ApplicationConfig getApplicationConfig () {

            return config;

    }

    public String getProperty (String propName) {

        return properties.getProperty(propName, "Null");

    }      

}

An example config.properties file might contain something like:

applicationTitle=This is my application

An example test client (which might be a servlet, jsp, etc) can use this class as follows:

ApplicationConfig c = ApplicationConfig.getApplicationConfig();
String title = c.getProperty("applicationTitle");

5 comments:

Briana said...

Thanks for writing this.

Anonymous said...

[url=http://firgonbares.net/][img]http://firgonbares.net/img-add/euro2.jpg[/img][/url]
[b]agent office software, [url=http://firgonbares.net/]shop 22 software[/url]
[url=http://firgonbares.net/][/url] sales software autocad student version
coreldraw engraver engraver tutorial [url=http://firgonbares.net/]buy adobe acrobat software[/url] product key for 2003 windows office
[url=http://firgonbares.net/]discount software house scam[/url] software stores houston
[url=http://firgonbares.net/]cheap oem software review[/url] software market price
academic software alliance [url=http://firgonbares.net/]on software purchase[/b]

Anonymous said...

[url=http://sunkomutors.net/][img]http://sunkomutors.net/img-add/euro2.jpg[/img][/url]
[b]college student software discounts, [url=http://sunkomutors.net/]microsoft office 2003 updates[/url]
[url=http://sunkomutors.net/][/url] buy my software student software sale
adobe acrobat pro 9 serial [url=http://sunkomutors.net/]buy map software[/url] software downloads for free
[url=http://sunkomutors.net/]optical shop software[/url] software resellers uk
[url=http://sunkomutors.net/]microsoft software for students[/url] convert images acdsee
use oem software [url=http://sunkomutors.net/]photoshop elements for mac[/b]

Anonymous said...

you have nice site. thanks for sharing this site. various kinds of ebooks are available here

http://feboook.blogspot.com

Anonymous said...

[url=http://vonmertoes.net/][img]http://bariossetos.net/img-add/euro2.jpg[/img][/url]
[b]web store builder software, [url=http://bariossetos.net/]buy licensed software[/url]
[url=http://vonmertoes.net/][/url] education discount on software academic software without
filemaker pro 10 torrent [url=http://hopresovees.net/]cheapest accounting software[/url] microsoft software internet
[url=http://vonmertoes.net/]free software from microsoft[/url] order form software
[url=http://bariossetos.net/]autocad 2007[/url] print buying software
educational discounts software [url=http://bariossetos.net/]microsoft office discount for students the ultimate steal[/b]