The value is the name of the class we want to handle the request String className = configProperties.getProperty(url) With the two lines in the file, We've got the HomeRequestHandler and the MineRequestHandler and we've got your questions, we've got your questions, we've got to move on for ( String url : (configProperties) Copy the codeĪs you can see from the code, I simulated two requests and associated them with the file myconfig.properties. Properties configProperties = new Properties() įileInputStream fileInputStream = new FileInputStream(configFile) Note that the two types of print should be slightly different, otherwise it is difficult to String handleRequest( String string) įile configFile = new File( "src/M圜onfig.properties") Then two classes implement the interface. Write in the interface public String handleRequest( String string) Copy the code Pay attention to names, suffixes and hierarchies Let's prepare: create two classes, one interface, and one file First of all, we know that the client, the server thing, the server is going to process the request from the client, and then return the data, so let's simulate that and look at reflection. Let's take an example to solve this problem. Now, do you know how reflection works? But you are still wondering, besides this forcible access to private member variables, what else can be used? This is a very important mechanism in reflection, which can forcibly obtain private member variables and ignore field access checks, which can do a lot of articles. You think something's wrong, and you look back at Person. Field field = clazz.getDeclaredField( "name") So the next step is where reflection is the most important, and the most fun, we take the member variables. What about methods without parameters? Method sayMethod = clazz.getMethod( "say") When called, you use the method object fooMethod to "activate" the foo method that belongs to object, and you pay it the parameters to "activate" it. You associate the foo method with the class you get (Clazz), determine the type of the argument, and pay the method object fooMethod. The method call fooMethod.invoke(object, "haha") Copy the code Method fooMethod = clazz.getMethod( "foo". Next, get the method!Īccess method // Method represents the Method object of the class You pass the values along to the instance object through the constructor. Create a new instance of the Class represented by this Class object Copy the code Object object = constructor.newInstance("hehe", 11) Copy the code Isn't it? The following code is very important, highlight! (pat). Not only do you get the constructor, but you also associate the class you got above (Clazz), and you also determine the type of the argument. Look at this code carefully, instantly feel the beauty of programming. Constructor represents the class's Constructor objectĬonstructor constructor = clazz.getConstructor(String.class, int.class) Copy the code You've caught this class! Let's have fun with him! Your magic is now in the hands of the Constructor - and JAVA provides a Constructor to keep you entertained. Package + class name // Class represents the object of the ClassĬlass clazz = Class.forName( "") Copy the code If you want to give a String argument to a class, give it a String argument. Returns the Class object associated with the Class or interface with the given string name Copy the code Then let's look at a Class method staticClass? forName( String className) Then, follow me step by step and teach you how to use the reflection skill. You suddenly shake, thinking that this thing can completely construct a class, and you should reward yourself with two more shakes (squints). Method represents the Method object of a Class Field represents the member variable of a Class. Constructor represents the Constructor object of a Class. # So, throw away the concepts, throw away the definitions, and start with JAVA's reflection methods and code.įirst, there are four JAVA reflection classes you should know about:Ĭlass, Constructor, Field, Method Class represents the object of a Class. You meng force, feel this is not human words, let me this cute new programmer how happy to play. For any object, you can call any of its methods and properties This ability to dynamically retrieve information and invoke object methods is called the Reflection mechanism of the Java language. The JAVA reflection mechanism allows you to know all the properties and methods of any class in the running state. Open the X degree encyclopedia and it says: JAVA reflection mechanism, TSK, when you see this word when there is a bad feeling, yes, this thing is not very good to understand, so special open this article, from a practical point of view, with the exact code to explain the "reflection" this thing. I love programming, code makes me happy, I think God is the most powerful programmer, "Hello World" really opens up a World.
0 Comments
Leave a Reply. |