The function of reflection & mdash; & gt; implements the framework function. Reflection implements the framework function.

Source: Internet
Author: User

Function of reflection-> implement the framework function, and implement the framework function through reflection

  • Core issues to be addressed by frameworks and frameworks
    • We sell a house to users for their own doors and windows and air conditioners. My house is a framework. users need to use my framework and insert the doors and windows into the framework I provide. The framework is different from the tool class. The tool class is called by the user class, while the framework is called by the user.
  • Core issues to be addressed by the Framework
    • When I am writing a framework (house), you may still be in elementary school and will not write programs yet? How can I call the framework program you wrote later?
 

             Class.forName(classNameStr).getMethod(methodName).invoke(obj,Class)

    • Because you cannot know the name of the class to be called when writing a program, you cannot directly create an instance object of a class in the program. Instead, you must use the reflection method.
  • Comprehensive Cases
    • First, use the new statement to create the instance objects of ArrayList and HashSet. This example uses eclipse to automatically generate the equals and hashCode methods of the ReflectPoint class,
 

Compare the running results of the two sets.

    • Then, create the ArrayList and HashSet instance objects by using the configuration file loading and reflection to compare and observe the running results.
    • Eclpse is introduced to explain how to manage resource files.
  • Properties class
    • The Properties object is equivalent to a HashMap. The <key, value> is installed in the memory.
    • Some functions are extended based on HashMap,
      • ① You can store key-value pairs in the memory to the hard disk.

 

      • ② During initialization, the key-value pairs in the file can be loaded into the properties object.
Package com. itcast. day1; import java. io. fileInputStream; import java. io. inputStream; import java. util. collection; import java. util. hashSet; import java. util. properties; import java. util. arrayList; public class ReflectTest3 {public static void main (String [] args) throws Exception {InputStream ips = new FileInputStream ("config. properties "); Properties props = new Properties (); props. load (ips); // load the hard disk to the memory, 2 from Load ips to the props object in the memory. close (); // close refers to releasing resources loaded by ips. If the ips object is not recycled by JVM as the close method is executed, a small memory leakage occurs. String className = props. getProperty ("className"); System. out. println (className); Collection collections =(Collection) Class. forName (className). newInstance ();ReflectPoint pt1 = new ReflectPoint (3, 3); collections. add (pt1); // put }}

Where is the config. properties file stored?

Compared with the current working path, this is hardly the case in actual projects!

 

The actual project must use an absolute path, but the absolute path is not hard-coded!

The actual project should:

The storage location of the config. properties file on the hard disk is specified by the user. You can configure "main directory"/config. properties.

GetRealPath (); // main directory/config. properties

 

  • ClassLoader -- ClassLoader

The class loader is usually used to load *. class files. Since. class files can all be loaded in (powerful !), Loading other configuration files is not a piece of cake! It depends on whether it is willing to load.

RefectTest3.class. geteClassLoader (). getResourceAsStream () // applicable to read-only configuration files

The configuration files of the framework are stored in the class path, because the configuration files read inside the framework are loaded using the class loader, and the class loader will find the files from the class path when loading the file.

    • The Class Loader loads config. properties, where config. propertis is stored in the class path.
Package com. itcast. day1; import java. io. file; import java. io. inputStream; import java.net. URL; import java. util. collection; import java. util. properties; public class ReflectTest3 {public static void main (String [] args) throws Exception {// find a file in the class path and convert it to the input stream InputStream ips = null; // ips = ReflectTest3.class. getClassLoader (). getResourceAsStream // ("com" + File. separator + "itcast" + File. separator + "day1" + File. separator + "config. properties "); // ips = ReflectTest3.class. getResourceAsStream ("config. properties "); // find the Class under this package // class provides a more concise way to load the configuration file (and Class. the newInstance method is omitted.Get the constructorIn the root directory of the class path, find ips = ReflectTest3.class. getResourceAsStream ("/" + "com" + File. separator + "itcast" + File. separator + "day1" + File. separator + "config. properties "); Properties props = new Properties (); props. load (ips); // 1. The hard disk is loaded to the memory, and 2 is loaded from the memory to the ips in the props object. close (); // close refers to releasing resources loaded by ips. If the ips object is not recycled by JVM as the close method is executed, a small memory leakage occurs. String className = props. getProperty ("className"); System. out. println (className); Collection collections = (Collection) Class. forName (className ). newInstance (); ReflectPoint pt1 = new ReflectPoint (3, 3); collections. add (pt1); // put it in System. out. println (collections. size ());}}
 

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.