Static Method of Dynamic execution class in Java

Source: Internet
Author: User

The static method of the dynamic execution class in Java. This article introduces how to use Java. lang. reflect. * The reflection mechanism provided by the package is used to construct class instances/execution class static methods during runtime.

 

/*
In Java, the class. forname () method can dynamically decide which class to load. This function is very useful for different implementations of the same interface. For example, for the database connection pool interface, we can have a variety of interface implementation classes to accomplish the same function. At the same time, you can simply modify the configuration file to specify which implementation class is actually used, read the configuration file information in the source code and use the class. forname (configclassname ). getinstance () can be used to construct a specific implementation class instance without modifying the source code each time. In this way, the program only needs to care about the interface definition, and the user only needs to set the configuration file to switch between different implementations of the same function.
However, if the implementation class needs to be initialized through static methods, the dynamic loading process will be more complex. Take the database connection pool as an example. Generally, the constructor of the connection pool is defined as private, and the unique instance is obtained through the custom getinstance () static method. In this case, the instance cannot be correctly constructed simply by using class. forname (). getinstance.
Fortunately, the reflection mechanism provided by Java provides us with a complete method to explore the internal structure of the class. Through the reflection mechanism, we can basically complete all the actions decided at runtime (although this implementation is more effective than other dynamic languages, such as PHP, Eval () ).

The following example shows how to use static methods of the dynamic runtime class.
-------------------------------------------------------
*/
Import java. Lang. Reflect .*;

Public class mytestclass {
Private Static object Plock = new object ();

Private Static mytestclass p_instance = NULL;
Private string s_configname = "";
Private Boolean B _isfromresource = true;

Public static object getinstance (string sconfigname,
Boolean bisfromresource ){
Synchronized (Plock ){
If (null = p_instance ){
P_instance =
New mytestclass (sconfigname, bisfromresource );
}
}
Return p_instance;
}

Private mytestclass (string sconfigname, Boolean bisfromresource ){
S_configname = sconfigname;
B _isfromresource = bisfromresource. booleanvalue ();
}

Public void echoinfo (){
System. Out. println ("current arguments: configname = [" +
S_configname + "], isfromresource = [" +
B _isfromresource + "]");
}

Public static void main (string [] ARGs) throws exception {
// Set the input parameter type of the method.
Class [] parametertypes = new class [] {
Java. Lang. String. Class,
Java. Lang. boolean. Class
};

Method mgetinstance = NULL;
String classname = "mytestclass ";

Class curtestclass = Class. forname (classname );
Try {
Mgetinstance = curtestclass.
Getmethod ("getinstance", parametertypes );
}
Catch (nosuchmethodexception e ){
E. printstacktrace ();
Mgetinstance = NULL;
}

If (mgetinstance! = NULL ){

Mytestclass pobj = (mytestclass)
Mgetinstance. Invoke (
Null,
New object [] {
"Src/myconfig. properties ",
Boolean. False
}
);

Pobj. echoinfo ();
}
Else {
Throw
New exception ("mytest init failed from Class" +
Classname +
System. getproperty ("line. seperator", "/N") +
"Method getinstance (string, Boolean) exists .");
}
}
}

 

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.