Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

config files for a webapplication - load once and store where?

I have a bunch of properties (configurations) that can change per environment. However these values do not change once the web application is deployed.So consider that there is an application.properties file that I want to read lots of times during normal program flow.

I know that I can probably load these at server startup time. However whats the best practice as far as accessing these from simple java classes at the backend? These business classes have nothing to do with servlets etc and have no dependencies on a webapp.

So today I load the properties via a ServletContext. Then what? Where should I keep them so as to be easily accessible to other objects without having to do a fileInputStream.load again?

Thanks.

like image 465
Kapsh Avatar asked Jun 30 '10 21:06

Kapsh


2 Answers

Implement a ServletContextListener.

Here's a basic kickoff example:

public class Config implements ServletContextListener {
    private static final String ATTRIBUTE_NAME = "config";
    private Properties config = new Properties();

    @Override
    public void contextInitialized(ServletContextEvent event) {
        try {
            config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
        } catch (IOException e) {
            throw new SomeRuntimeException("Loading config failed", e);
        }
        event.getServletContext().setAttribute(ATTRIBUTE_NAME, this);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // NOOP.
    }

    public static Config getInstance(ServletContext context) {
        return (Config) context.getAttribute(ATTRIBUTE_NAME);
    }

    public String getProperty(String key) {
        return config.getProperty(key);
    }
}

which you register as follows in web.xml:

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

and which you can access in your servlets as follows:

Config config = Config.getInstance(getServletContext());
String property = config.getProperty("somekey");

After having a second thought, those properties are thus 100% specific to business layer, not to the webapplication itself? Then a ServletContextListener is indeed clumsy and too tight coupled. Just give the business layer its own Config class which loads the properties from the classpath and caches it in some static variable (Map<String, Properties> maybe?).

like image 195
BalusC Avatar answered Sep 20 '22 11:09

BalusC


Put your configuration classes/properties in a jar file, and put that jar file in WEB-INF/lib. Then you can access them through the normal classpath resource facilities whereever you need to in your web application.

like image 32
Thorbjørn Ravn Andersen Avatar answered Sep 18 '22 11:09

Thorbjørn Ravn Andersen