?
Solved

look up/reference information information in spring application

Posted on 2012-09-16
3
Medium Priority
?
364 Views
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

thanks
0
Comment
Question by:dhuma
  • 2
3 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 38404460
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.
0
 

Author Comment

by:dhuma
ID: 38415524
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

someSingleton.getValue("code");

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.
0
 
LVL 36

Accepted Solution

by:
mccarl earned 1500 total points
ID: 38416578
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!)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 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