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 ();
}
}