Skip to main content

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");

Comments

Anonymous said…
Thanks for writing this.
I have no words for this great post such a awe-some information i got gathered. Thanks to Author.

Popular posts from this blog

Interfacing C# .Net and R - Integrating the best of both worlds

In specific software areas like in quantitative finance or else in other mathematical domains, data centric programming typically requires a good balance between three requirements - (1) a solid platform with rich mathematical/statistical functionality (2) having an easy to use, contemporary, programming environment which permits easy and flexible front end code development and (3) an easy to use interface between the two environments. In this artcile I am going to explain how such a balance can be attained by using two of the best products in their specific worlds - using the rich R library as the mathematical/statistical component but then interfacing with C# for the front end application design. As an interfacing option I banked on using R (D)COM which provides an easy to use interfacing method which keeps you away from spending hours identifying interfacing problems. The software required for this tutorial is the following: 1. R software ( download from here ) 2. R (D)COM Interf

Interfacing C# .Net and R - Integrating the best of both worlds (Part 2)

This post is a continuation from the previous post ( Part I ) focusing on interfacing C# with R using the R (D)COM. In this post I am going to enhance my previous exercise by creating a Facade .Net Class which facilitates access to specific functions in R. Creating the R Facade Class Creating a Facade Class (or a set of .Net classes) which acts as a .Net wrapper to R functions greatly facilitate the use of R functions and their integration within the .Net programming environment. Below I am showing an excerpt from the class RFacade that I have created in this example. using System; using System.Collections.Generic; using System.Linq; using System.Text; using StatConnectorCommonLib; using STATCONNECTORSRVLib; using System.Runtime.InteropServices; namespace R { class RFacade : IDisposable {      private StatConnector rconn;      private bool disposed = false;      public RFacade()      {           rconn = new STATCONNECTORSRVLib.StatConnector();           rcon

Simple moving average trading strategy using Python

Hi All, I am presenting simple boiler point code that can quickly be applied to test technical indicator strategies using Python. The code: 1. downloads daily stock data from google, 2. calculates the short and long moving averages 3. generates the trading signals 4. calculates the daily returns 5. runs the moving average strategy and calculates the cumulative return 6. plots cumulative return of our simple strategy Here is the code ... enjoy trying it out and extend it as required: import numpy as np import pandas_datareader as datar import datetime import matplotlib.pyplot as plt date_start = datetime.datetime( 2017 , 1 , 1 ) date_end = datetime.datetime( 2017 , 6 , 30 ) data = datar.get_data_google( 'AAPL' , date_start , date_end) short_ma = 5 long_ma = 20 data[ 'short_ma' ] = data[ 'Close' ].rolling(short_ma).mean() data[ 'long_ma' ] = data[ 'Close' ].rolling(long_ma).mean() data[ 'masig' ] = data[ 'short_ma'