Dark Horse programmer------java-Framework, Reflection Realization Framework Principle Learning Summary

Source: Internet
Author: User

--------------------------Java Training, Android training, look forward to communicating with you! ---------------------------

The concept of framework and the principle of reflection implementation Framework Learning Summary

First, the concept

1. Frame:

  Technical implementation of calls to currently undefined or non-implemented objects through the reflection technology of Java.

  For example: Developers build houses to sell to customers, and customers install windows and doors, and developers build houses that are a framework, and buyers install doors and windows they need on the frame of the house. The difference between a framework and a tool class is that the framework calls the user-supplied class, and the tool class is called by the user's own class.

2. Core issues to be solved by the framework

When writing a frame (house), how can the framework program now write to a class (windows and doors) that the programmer has not written? Because there is no way to know the name of the class to be called when the program is written, it is not possible to make an instance object of a class directly in the program, but to do so by reflection.

3, the Simple framework procedure steps:

1) Right-click the project file name a profile such as: Config.properties, and then write the configuration information. such as the key-value pair: Classname=java.util.arraylist, the right-hand configuration key, and the value on the right.

2) Code implementation, loading this file:

① reads the file into the read stream and writes out the absolute path to the configuration file.

such as: InputStream is=new fileinputstream ("configuration file");

② uses the Properties class's load () method to store the data in the stream into a collection.

③ closed stream: The read stream is closed because the data in the stream is already loaded into memory.

3) Obtain the classname by using the GetProperty () method, that is, the configured value, that is, a class name.

4) Create the object Newinstance () in a reflective manner.

5) Execute the program body function

Second, class loader

1, Summary: Class loader is to load the. class file into memory, or you can load the information in the normal file into memory.

2, the file loading problem:

1) Eclipse compiles all. java files from the source program into a. class file and then puts it in the directory specified by Classpath. The non-. Java files will be copied intact into the directory specified in the. class. At run time, the. class file is executed.

2) Put the configuration files together in the. class file directory and the ClassLoader will load together.

3. Load the resource file: Use the class loader.

1) loaded into memory by the ClassLoader ClassLoader, which takes the class loader with the getClassLoader () method and then loads the configuration file (resource file) into memory using the class loader's getResourceAsStream (String name) method. Using the ClassLoader to load the configuration file, you need to write the package name that is placed on the configuration file. This method is only read function.

2) class also provides the getResourceAsStream method to load the resource file, in fact, it is called the ClassLoader method inside. At this point, the configuration file is relative to the current directory of the class file, that is, in this way, the configuration file can be omitted before the package name.

such as: Class name. Class.getresourceasstream ("Resource file name")

4, the path of the configuration file problem:

1) Use the absolute path, through the Getrealpath () method to calculate the specific directory, rather than the internal code out.

Generally first get the user-defined total directory, in addition to their own internal path. You can get the file path through the Getrealpath () method. A configuration file modification is required to be stored in a configuration file, so the absolute path to it is needed, so the configuration file is placed inside the program.

2) name path problem:

① if the configuration file and the Classpath directory do not matter, you must write the absolute path,

② if the configuration file and the Classpath directory are related, that is, in the Classpath directory or in its subdirectories (typically the Resource folder Resource), then the relative path must be written because it knows for itself which package it belongs to, relative to the current package.

Example:

1  PackageCom.itheima.day1;2 3 ImportJava.io.InputStream;4 Importjava.util.Collection;5 Importjava.util.Properties;6 7  Public classmasstests {8      Public Static voidMain (string[] args)throwsexception{9         //You should use ArrayList and hashset directly before introducing a read from the configuration file,TenProperties props =NewProperties (); One         //show the problem of relative path first A         //inputstream ips = new FileInputStream ("Config.properties"); -         /*a class loader can load a. class file, which of course can also load other files in the CLASSPATH environment, since it has so much power that it has no reason not to provide such a method in passing.  - * It can also only load those files in the CLASSPATH environment. Note: When you use the ClassLoader directly, you cannot begin with/. */ the         //inputstream ips = ReflectTest2.class.getClassLoader (). getResourceAsStream ("Cn/itheima/demo/config.properties" ); -         //Class provides a convenient way to load a file in the same package directory with the same classloader that loads the current class -         //inputstream ips = ReflectTest2.class.getResourceAsStream ("config.properties"); -InputStream ips = masstests.class. getResourceAsStream ("/cn/itheima/demo/config.properties"); + props.load (IPS); - ips.close (); +  AString className = Props.getproperty ("ClassName"); atClass Clazz =Class.forName (className); -  -Collection Collection =(Collection) clazz.newinstance (); -Reflectpoint point1 =NewReflectpoint (5,5); -Reflectpoint Point2 =NewReflectpoint (5,5); -Reflectpoint Point3 =NewReflectpoint (6,4); in          - Collection.add (point1); to Collection.add (point2); + Collection.add (POINT3); - Collection.add (point1); the          *         //Point1.setx (5); $         //Collection.remove (point1);Panax Notoginseng System.out.println (Collection.size ()); -         } the}

Dark Horse programmer------java-Framework, reflection Implementation Framework Principle learning summary

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.