Tomcat parsing XML and reflection to create objects principle

Tomcat parsing XML and reflection to create objects principle

  1 import java.lang.reflect.InvocationTargetException;
  2 import java.lang.reflect.Method;
  3 import java.util.List;
  4 
  5 import org.dom4j.Document;
  6 import org.dom4j.DocumentException;
  7 import org.dom4j.Element;
  8 import org.dom4j.io.SAXReader;
  9 
 10 public class ServerReadXML1 {
 11 
 12     public static void main(String[] args)
 13             throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException,
 14             A NoSuchMethodException, a SecurityException, an IllegalArgumentException, a InvocationTargetException {
 15  
16          // now Servlet If the input in a browser-pattern URL 
. 17          String urlPattern = " / First " ;
 18 is  
. 19          // The class name acquired urlPattern 
20 is          String className = getClassByUrl (urlPattern );
 21 is  
22 is          // Get class object based on the full class name 
23 is          class = clazz the Class.forName (className);
 24  
25          // create the specified object reflected by the object clazz 
26 is          Object obj = clazz.newInstance ();
 27 
28          // Get service method 
29          Method, Method clazz.getDeclaredMethod = ( " service " );
 30  
31 is          // Get Permissions 
32          method.setAccessible ( to true );
 33 is  
34 is          // perform service method 
35          Method.invoke (obj);
 36  
37 [      }
 38 is  
39      Private  static String getClassByUrl (String urlPattern) throws DocumentException {
 40  
41 is          // 1. Create an object SAXReader 
42 is          SAXReader Reader = new newSAXReader ();
 43 is  
44 is          // 2. read the file 
45          the Document Document reader.Read = (ServerReadXML1. Class .getClassLoader () the getResourceAsStream (. " The web.xml " ));
 46 is  
47          // 3. Get root 
48          the Element = the rootElement document.getRootElement ();
 49          // System.out.println (rootElement.getName ());
 50  
51 is          // 4. acquisition sub-nodes below the root node 
52 is          List <the Element> servletList = rootElement.elements ();
 53 is  
54 is          // content recorded in the same servlet-name tag urlPattern 
55         ServletName = String "" ;
 56 is  
57 is          // recorded content servlet-class of tag servlet
 58          // content of servletClassName Servlet is full class name 
59          String servletClassName = "" ;
 60  
61 is          // 5. The traverse the child nodes 
62          for (the Element servletElement: servletList) {
 63 is              // System.out.println (servletElement.getName ());
 64  
65              // determines if the label is a servlet-mapping, the execution code 
66              IF ( " servlet-mapping " .equals ( servletElement.getName ())) {
 67  
68                 // Get the object tag url-pattern 
69                  the Element URL = servletElement.element ( " url-pattern " );
 70  
71 is                  // determine the content of the tag and the values are the same urlPattern 
72                  IF (urlPattern.equals (url.getText () )) {
 73 is  
74                      // record the same content urlPattern servlet-name tag
 75                      // if the same, then the recording servletName
 76                      // content acquisition servlet-mapping of the servelt-name 
77                      servletName = servletElement.element ( " the servlet -name " ) .getText ();
 78  
79                  }
 80 
81              }
 82  
83          }
 84  
85          // iterate again to 
86          for (the Element servletElement: servletList) {
 87              // determines if the servlet label, this code is executed 
88              IF ( " servlet " .equals (servletElement.getName ())) {
 89  
90                  // value judgment on a traversal acquired servletName and the content traversing the servlet-name is the same as 
91                  iF (servletName.equals (servletElement.element ( " servlet-name " ) .getText ())) {
 92  
93                      // if the same recording servletClassName 
94                     servletClassName = servletElement.element("servlet-class").getText();
 95 
 96                 }
 97 
 98             }
 99 
100         }
101 
102         // 返回Servlet的全类名 servletClassName
103         return servletClassName;
104     }
105 
106 }

 

 

1. Get Class 4 ways reflected

@Test
    public void test1() throws ClassNotFoundException {
        
        //1.类名.class
        Class clazz = String.class;
        System.out.println(clazz);
        
        //2.对象.getClass()
        Class clazz1 = "abc".getClass();
        System.out.println(clazz1);
        
        //3.Class.forName();
        Class clazz2 = Class.forName("java.lang.String");
        System.out.println(clazz2);
        
        //4.ClassLoader .loadClass("全类名")
        Class clazz3 = ReflectTest1.class.getClassLoader().loadClass("java.lang.String");
        System.out.println(clazz3);
        
        
    }

 

2.反射使用属性的常用方法

@Test
    public void test2() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        
        //获取Class对象 可以获取其内部的属性
        Class clazz = Class.forName("com.atguigu.bean.User");
        
        User user = new User();
        
        //Field对象 代表中类的属性 getField只能获取公共属性
        Field field = clazz.getField("email");
        System.out.println(field);
        
        
         //此种方式破坏代码的封装性 不推荐使用
        Field field2 = clazz.getDeclaredField("id");
        System.out.println(field2);
        
        field2.setAccessible(true);
        field2.setInt(user, 1001);
        System.out.println(user);
        
        
    }

3.反射使用方法的常用方法

@Test
    public void test3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
        
        Class clazz = Class.forName("com.atguigu.bean.User");
        
        //通过反射创建对象
        Object obj = clazz.newInstance();
        
        //现在想要设置name值
        String fileName = "name";
        
        //创建一个方法名
        String methodName = "set" + fileName.substring(0, 1).toUpperCase() //N
        + fileName.substring(1).toLowerCase(); //ame
        
        //根据方法名 获取公共方法
        Method method = clazz.getMethod(methodName, String.class);
        
        //执行指定的方法
        method.invoke(obj, "yangjian");
        
        
        System.out.println(obj);
    }

 

Guess you like

Origin www.cnblogs.com/zmy-520131499/p/11319482.html