The introspection of java foundation is strengthened

The introspection of java foundation is strengthened

===================================================== ============================ 
Introspection (Introspector class) — > JavaBean 
 -------------- -------------------------------------------------- ------------- 
First, why study introspection?
    When developing a framework, it is often necessary to use the properties of java objects to encapsulate program data.
    It is too troublesome to use reflection technology to complete such operations every time, so SUN has developed a set of APIs, which are specially used to manipulate the properties of java objects.
-------------------------------------------------- --------------------------- 
Second, what is the read and write method of JavaBean and properties ?
    A bean is a class, and a javabean is a java class.
-----------------------------------------------------------------------------
3. There are two ways to access JavaBeans through introspection technology (the java.beans package provides an introspective API):
    1. Manipulate Bean properties through PropertyDescriptor class
     2. Obtain BeanInfo of Bean object through Introspector class, BeanInfo encapsulates all properties of a class.
        Then get the property descriptor object PropertyDescriptor through the BeanInfo object,
        Through this property descriptor object, a property object can be obtained, and then the getter / setter method can be called through the reflection mechanism.
        (Note: In reflection, member variables are called fields, and member methods (specifically getter / setter methods) are called properties)
        
        PropertyDescriptor, as the name suggests, means property description. It quickly manipulates JavaBean's getter / setter methods through reflection.
        Important method:
            Method getWriteMethod() Gets the setter method and returns the Method object
            Method getReadMethod() Get the getter method and return the Method object
--------------------------------------                    
        Student stu = new Student();
         // Get specified The property object 
        PropertyDescriptor pd = new PropertyDescriptor("name", Student.class ) ;
        Method setter = pd.getWriteMethod(); // Get the setter method: setName() 
        setter.invoke(stu, "tom" );
        
        Method getter = pd.getReadMethod();  // 得到getter方法:getName()
        System.out.println(getter.invoke(stu, null));
--------------------------------------
        Look at the above code and think about a question: Since I have instantiated the Student class, why not call its setName method directly?
        In fact, the correct usage should be: complete decoupling from the Student class . (That is to say, the student class will not appear in the code I wrote, and the student class will be placed in the configuration file)
        
        Class cls = Class.forName("cn.itcast.Student");
        Object obj = cls.newInstance();
        PropertyDescriptor pd = new PropertyDescriptor("name", cls);
        Method m = pd.getWriterMethod();
        m.invode(obj, "tom");
      
        m = pd.getReadMethod();
        System.out.println((String) m.invoke(obj));
-------------------------------------------------- ---------------------------         
Fourth, Beanutils toolkit 
    The Apache organization has developed a set of APIs for manipulating JavaBeans. This set of APIs takes into account many application scenarios in actual development.
    Therefore, in actual development, many programmers use this API to operate JavaBeans to simplify the writing of program codes.
    
    Common classes of the Beanutils toolkit:
        BeanUtils
        PropertyUtils
        ConvertUtils.regsiter(Converter convert, Class clazz)
        custom converter
--------------------------------------        
    Need to rely on two jar packages:
        commons-beanutils-1.8.3.jar
        commons-logging-1.1.1.jar
--------------------------------------        
    Example:
        Student stu = new Student();
        
        BeanUtils.setProperty(stu, "name", "Zhang San");        // Assign the property 
        String s = BeanUtils.getProperty(stu, "name");    // Get the property value
        
        System.out.println(s);
--------------------------------------        
        Student stu = new Student();    
        
        BeanUtils.setProperty(stu, "age", "18"); // BeanUtils supports 8 basic data types by default, and automatically converts 
        String s = BeanUtils.getProperty(stu, "age" );
        
        System.out.println(s);
--------------------------------------
        Student stu = new Student();
        
        // Register type converter 
        ConvertUtils.register( new DateLocaleConverter(), Date.class ) ;
        
        BeanUtils.setProperty(stu, "birthday", "1990-11-12"); // Assign property 
        String s = BeanUtils.getProperty(stu, "birthday" );
        
        System.out.println(s);
--------------------------------------
        Student stu = new Student();
        
        // Register type converter 
        ConvertUtils.register( new Converter() {
            @Override
            public Object convert(Class type, Object value) {
                
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                if (value instanceof String) {
                    String v = (String) value;
                    try {
                        return sdf.parse(v);
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                }
                
                return null;
            }
        }, Date.class);
        
        BeanUtils.setProperty(stu, "birthday", "1995-11-12"); // Assign property 
        String s = BeanUtils.getProperty(stu, "birthday" );
        
        System.out.println(s);
--------------------------------------    
        Map m = new HashMap();
        m.put( "name", "Zhang San");   // The key name must be the same as the variable name in the object 
        m.put("age", "18");     // The key name must be the same as the variable name in the object The variable name is the same 
        m.put("birthday", "1992-05-12"); // The key name must be the same as the variable name in the object 
        
        Student stu = new Student();
        
        // Register type converter 
        ConvertUtils.register( new DateLocaleConverter(), Date.class ) ;
        BeanUtils.populate(stu, m); // Automatically fill the Map attribute into the Bean, this sentence is often used when learning the framework later
        
        System.out.println(stu.getName());
=============================================================================

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325120616&siteId=291194637