look up/reference information information in spring application

Posted on 2012-09-16
Last Modified: 2012-09-21
we have a spring application with dependencies configured. We would like to make some reference/lookup information to be available to various modules in the application,
example: country code -- country description
                currency code -- currency description
                application specific codes ----- description

what is the good design pattern to load this information at the application start up and make it available to all modules in the application

Question by:dhuma
    LVL 35

    Expert Comment

    What do you mean by modules of your application? At what level is your application modularised?

    I don't know if you are talking about a separate module per JVM or just a separate module per xml config file. And obviously, this distinction has a big effect on the answer to your question.

    For the purposes of providing one possible answer, I will assume that you just have a number of xml config files, where each one contains the beans the each 'module' that you refer to. If so, then what you can do is use the fact the context's can be formed into heirarchies. So you can have a different xml config file to load beans that make available your lookup information, and then when you create each 'module' by initialising an ApplicationContext with it's xml config, at this point you can specify a 'parentContext' which is this first context and then all the beans in each 'module' context can access the parent context's lookup beans. Note, nothing in this parent context can access anything in any of the child contexts, and likewise, no child context can access any other child context.

    Author Comment

    In general , most of the applications need to access some reference/lookup information,

    for example, if we need the description for a given code, we might say


    this singleton would pull data from some database tables at the startup and make it available to all the classes in the application

    the application that we use is configured in spring, however, the class from which i would like to access this reference information is no instantiated by spring. Spring application context is not available in this class.
    LVL 35

    Accepted Solution

    the class from which i would like to access this reference information is no instantiated by spring

    Ok then, it's just that since the original question mentioned Spring a number of times, I assumed that you would be using it to instantiate your classes as that is its main use.

    Anyway, if you are using Spring, probably the other way that you would go about this is to have a class with purely 'static' methods/members. You have a static initializer that does the DB lookup and stores the reference/lookup info in a static object (probably a Map) and then a static getValue method that you use to retrieve the lookup value for a key. Something like this...

    public class MyAppInfo {
      private static Map<String, Object> info = new HashMap<String, Object>();
      static {
        // populate the info map from the database
        // eg..
        info.put("code", "description");
      private MyAppInfo() {}    // private default constructor; makes it explicit that you don't instantiate this class
      public static Object getValue(String key) {
        return info.get(key);

    Open in new window

    (Note: the above hasn't actually been compiled and so there may be typos, but the idea is there!)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    zeroFront challenge 7 58
    classnotfoundexception 3 28
    Convert BufferedReader to File 1 49
    mapShare challenge 13 40
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now