Design mode 1.1 again on reflection and Java in the configuration file to get the class name, and the method of generating objects

Source: Internet
Author: User
Tags reflection try catch

Feel in the design pattern, many places have to use the reflection. In particular, read the information from the configuration file and use this information to dynamically load. The application of reflection is particularly common, where reflection and related concepts are discussed once again.

Class loading mechanism for ClassLoader:

L Java classes are not loaded into memory at once

L is only dynamically loaded into memory when needed, that is, dynamic loading during run time.

L A static statement is executed once after loading, and executes once

Dynamic statement blocks are executed each time new objects are newly.

There are many ClassLoader in Java:

Bootstrap class loader The top level of CL, responsible for managing class. It may have been written in C or by sinks. User access is not allowed.

Extension class Loader

Application class Loader

Other class loaders

n Secure class Loader

n URL clas Loader

Feel or read the name of the class in the configuration file, reflection is most common.

1. Location of the property file:

Don't write to death in a directory

Do not use a relative path (consider the situation in the jar package)

The most commonly used position, put in the classpath inside. Can be accessed by properties. In eclipse, it is embodied in the SRC directory. SRC compiles good things, and will be classpath by eclipse in the current project, so it can be accessed by properties.

2. Contents of the property file:

Observers=com.bjsxt.dp.observer.dad,com.bjsxt.dp.observer.grandfather,com.bjsxt.dp.observer.dog

Observers is the key.

Com.bjsxt.dp.observer.dad,com.bjsxt.dp.observer.grandfather,com.bjsxt.dp.observer.dog is a value, separated by commas in the middle.

3. Among the users, the information in the configuration file is obtained through a Propertymgr helper class.

class Propertymgr {

Use System objects, properties, require configuration files to be placed inside classpath

privatestatic Propertiesprops =new Properties ();

Static {

Try {

Find configuration files through properties

Note that you must use a try catch here.

Props.load (Test.class. getClassLoader (). getResourceAsStream ("Com/bjsxt/dp/observer/observer.properties "));

Catch (IOException e) {

E.printstacktrace ();

}

}

Gets the value of the property by GetProperty. Is in the configuration file, the back section

publicstatic Stringgetproperty (String key) {

return Props.getproperty (key);

}

}

In fact, the value of the property has not yet been completed and a string is obtained. You need to continue to get each class by using the string split method. The objects of these classes are then generated.

A string obtained from properties, treated with split

String[] observers = Propertymgr.getproperty ("observers"). Split (",");

For each class, the object is generated, notice that you want to try Catch

for (String s:observers) {

Try {

C.addwakenuplistener ((Wakenuplistener) (Class.forName (s). newinstance ());

Catch (Instantiationexception e) {

E.printstacktrace ();

Catch (Illegalaccessexception e) {

E.printstacktrace ();

Catch (ClassNotFoundException e) {

E.printstacktrace ();

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.