Gets all the properties in the class Bean:
@Test//get all properties in a class bean Public voidTest1 ()throwsexception{BeanInfo Info= Introspector.getbeaninfo (person.class); Propertydescriptor[] Decriptors=info.getpropertydescriptors (); for(PropertyDescriptor decriptor:decriptors) {//the name of the output propertySystem.out.println (Decriptor.getname ()); //types of output PropertiesSystem.out.println (Decriptor.getpropertytype ()); } }
Read/write a property in the bean:
@Test//manipulating a property in a bean Public voidTest2 ()throwsexception{Person P=NewPerson (); PropertyDescriptor Decriptor=NewPropertyDescriptor ("username", person.class); //get the Write method of the attributeMethod method=Decriptor.getwritemethod (); Method.invoke (P,"Zhang San"); //get the property Read methodMethod=Decriptor.getreadmethod (); String username=(String) Method.invoke (p); SYSTEM.OUT.PRINTLN (username);}
[Novice Java] use introspection (introspector) to manipulate JavaBean properties