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