Java Reflection mechanism usage scenarios

Source: Internet
Author: User
Tags tomcat server

1 ImportJava.io.*;2 Importjava.util.Properties;3 4 /*Problem Description: There is a motherboard-already defined, do not want to modify its code, and want to add some other features on the motherboard? 5 * Problem Solving Method: The motherboard provides an interface for other devices to implement the interface at the time of definition, and the device's6 * Information is written in the configuration file. The motherboard defines the time to read the profile information, dynamically obtains information from other devices, and uses7 * other functions. This will not modify the motherboard code at all. Greatly improves the extensibility of the program. 8 * So the object of the extended class can be obtained dynamically by reflection, only the content of the specific configuration file needs to be modified. 9  *Ten * Reflection: Dynamically gets the information of the class. In fact, through the class name, load the class, get the Class object, and then do other operations.  One * The greatest benefit of using reflection is that the scalability of the program is greatly improved.  A * Reflection-related classes: Method,field,constructor support inside the Class,java.lang.reflect package -  * */ -  Public classusereflection { the  -      Public Static voidMain (string[] args)throwsIOException, ClassNotFoundException, Exception { -mainboard MB =Newmainboard (); - Mb.run (); +          -         //write the added device in the configuration file and encapsulate the file as an object +File f =NewFile ("Pci.properties"); A         //associating a configuration file to a stream atFileInputStream FIS =NewFileInputStream (f); -         //Create a Properties object -Properties prop =NewProperties (); -         //load the information in the stream into the prop - prop.load (FIS); -         //traverse the information obtained from the stream in          for(inti = 0; I < prop.size (); i++){ -String name = Prop.getproperty ("PCI" + (i+1)); toClass Clazz =class.forname (name); +PCI p =(PCI) clazz.newinstance (); - Mb.usepci (p); the         } *          $ Panax Notoginseng     } -  the } +  A classmainboard{ the      Public voidrun () { +System.out.println ("Mainboard run-------"); -     } $      $      Public voidUsepci (PCI p) { -         if(P! =NULL){ - P.open (); the p.close (); -         }Wuyi     } the}
Public interface PCI {public void open ();p ublic void Close ();} Class SoundBoard implements Pci{public SoundBoard () {} @Overridepublic void Open () {System.out.println ("open Sound");} @Overridepublic void Close () {System.out.println ("close Sound");}} Class Wifiboard implements Pci{public Wifiboard () {} @Overridepublic void Open () {System.out.println ("open WiFi");} @Overridepublic void Close () {System.out.println ("close WiFi");}}

Above is a simulation of the use of reflection mechanism, a more common example of the real use of reflection mechanism is the Tomcat--web server, the role of the Tomcat server: is to handle the browser request

and give the appropriate response, but the browser's request type is usually different, in order to improve the scalability of the server, usually when Tomcat is written to provide an external public interface--servlet, how to

Response and processing requests, what the developer needs to do is to implement the interface directly, define appropriate processing, and write the implementation class information in the configuration file, which is read by Tomcat at the time of operation

The content of the configuration file, the dynamic access to appropriate processing, processing browser requests, the developer's programming difficulty is also reduced, directly facing the interface and configuration files. If you want to modify the specific processing method, directly

Modify the contents of the configuration file.

Java Reflection mechanism usage scenarios

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.