loading classes from config file

Hi Guys,

I have the following requirement.

(1). Have a XML configuration file which has values like below.

<entry key="CustomerService">com.web.CustomerService</entry>

I have to read this XML file and load the appropriate Web service. Is it good idea to load classes from config files? If this config file has 30 classes to load, then how abt the performance? I am loading the class like below.

Properties prop = new Properties();

FileInputStream fis =new FileInputStream("properties.xml");

prop.loadFromXML(fis);

String className = prop.getProperty("AccountWebService");

Class service = Class.forName(className);

service.newInstance();

[717 byte] By [Accenture9a] at [2007-10-3 2:40:12]
# 1

> Hi Guys,

>

> I have the following requirement.

>

> (1). Have a XML configuration file which has values

> like below.

> <entry

> key="CustomerService">com.web.CustomerService</entry>

>

> I have to read this XML file and load the appropriate

> Web service. Is it good idea to load classes from

> config files?

You won't be loading any classes.The class loader does that.

Spring uses XML to load application contexts. Struts has a config file for its Actions. If it's good enough for them, I'm guessing it's good enough for you.

You'll be reading the XML file and caching the results. Looks like a lookup for services.

> If this config file has 30 classes to

> load, then how abt the performance? I am loading the

> class like below.

I think the example code you give is a terrible idea.

Your services should be interfaces. That also means that they should be stateless and thread-safe - no private data members. If that's the case, I'd recommend that you load the properties file in your servlet's init() method, instantiate the service classes once on startup, and cache them in a Map that uses the key to return a Service instance. You'll pay the cost of instantiating the services on startup and reuse them each time as requests come in. You'll also keep the number of objects in memory down that way.

This assumes a front controller servlet that handles all HTTP requests.

It might be a good idea to synchronize access to that Map of services to make sure that you don't end up with race conditions.

Sounds like you've rolled your own Struts or Spring MVC for the web tier. Why not use a proven web framework instead of your own?

%

duffymoa at 2007-7-14 19:38:30 > top of Java-index,Java Essentials,Java Programming...