Java Learning within province

Source: Internet
Author: User

Reflection and introspection to solve the coupling problem

1  PackageCom.gh.introspector;2 /**3 * JavaBean4  * @authorGanhang5  *6  */7  Public classDog {8     PrivateString name;9     Private intAge ;Ten      PublicString GetName () { One          A         returnname; -     } -      Public voidsetName (String name) { the          This. Name =name; -     } -      Public intGetage () { -         returnAge ; +     } -      Public voidSetage (intAge ) { +          This. Age =Age ; A     } at @Override -      PublicString toString () { -         return"Dog [name=" + name + ", age=" + Age + "]"; -     } -      -}
1  PackageCom.gh.introspector;2 3 ImportJava.beans.BeanInfo;4 ImportJava.beans.Introspector;5 ImportJava.beans.PropertyDescriptor;6 Importjava.io.IOException;7 ImportJava.io.InputStream;8 ImportJava.lang.reflect.Method;9 Importjava.util.Properties;Ten  One /** A * Factory class -  * @authorGanhang -  *  the  */ -  Public classDogfactory { -     //Properties File Operations Tool class -     Private StaticProperties config =NewProperties (); +     Static { -         //read the properties file to the input stream +InputStream is =Thread.CurrentThread () A. Getcontextclassloader (). getResourceAsStream ("Bean.properties"); at         //Note: The configuration file under the SRC can not write the package name, if the package inside to write the package name, no person under the null pointer -         Try { -             //Load Input stream - Config.load (IS); - is.close (); -}Catch(IOException e) { in e.printstacktrace (); -         } to     } +     //also do not want to be coupled with the dog, directly dependent, so use the configuration file to create the dog -      Public StaticDog Getdog (String name) { the          *         //get values by key $String classname =Config.getproperty (name);Panax Notoginseng         Try { -             //get class information based on class name class object theClass Dogclass =Class.forName (classname); +             //instantiating an object ADog dog =(Dog) dogclass.newinstance (); the             //introspection: Getting bean information +BeanInfo BeanInfo =Introspector.getbeaninfo (dogclass); -             //Get all property descriptors (arrays) with bean information $Propertydescriptor[] PDS =beaninfo.getpropertydescriptors (); $             //looping through property descriptors -              for(PropertyDescriptor Pd:pds) { -                 if("Name". Equals (Pd.getname ())) { theString namevalue = Config.getproperty ("Dog.name"); -                     //to obtain a write operation method (set method) on the modified attribute through the property descriptorWuyiMethod method =Pd.getwritemethod (); the                     //calling a method on a dog object - Method.invoke (dog, namevalue); Wu}Else if("Age". Equals (Pd.getname ())) { -String Agevalue = Config.getproperty ("Dog.age"); About                     //to obtain a write operation method (set method) on the modified attribute through the property descriptor $Method method =Pd.getwritemethod (); -                     //call the method on the dog object, note that string goes int - Method.invoke (dog, Integer.parseint (Agevalue)); -                 } A             } +             returnDog; the}Catch(Exception e) { - e.printstacktrace (); $         } the         return NULL; the     } the}

Configuration file Bean.properties

#\u914d\u7f6ejavabean\u7684\u5168\u540d
Dog=com.gh.introspector.dog
dog.name=\u5c0f\u767d
Dog.age=3

Java Learning within province

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.