1.首先写一个BEAN类 public class Text ... { public Text() ...{ } private String text1; private String text2; public String getText1() ...{ return text1; } public void setText1(String text1) ...{ this.text1 = text1; } public void setText2(String text2) ...{ this.text2 = text2; } public String getText2() ...{ return text2; }} 2.开始实现类 import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.io.File; public class Test2 ... {// public static void main(String args[]){// LoanBeanInfo bean = new LoanBeanInfo();// Beans bs = new Beans();// bs.setName("jiao");// PropertyDescriptor[] ss = bean.getPropertyDescriptors("name",bs.getClass());// for(int i=0;i<ss.length;i++){// PropertyDescriptor pd = ss[i];// // System.out.println(pd.);// System.out.println(bs.);// }// } public static void main(String[] args) ...{ // 实例化要注入信息的类 Text text = new Text(); BeanInfo beanInfo = null; try ...{ beanInfo = Introspector.getBeanInfo(text.getClass()); } catch(IntrospectionException e) ...{ } PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); java.util.HashMap desc = new java.util.HashMap(); for(int i = 0; i < descriptors.length; i++) ...{ desc.put(descriptors[i].getName().toLowerCase(), descriptors[i]); } for(int i = 1; i < 3; i++) ...{ // 规律变量名生成 String key = "text" + i; String value = "aaa" + i; PropertyDescriptor pd = (PropertyDescriptor)desc.get(key.toLowerCase()); if(pd == null) continue; java.lang.reflect.Method tt = pd.getWriteMethod(); try ...{ tt.invoke(text, new Object[]...{value}); } catch(Exception ex) ...{ ex.printStackTrace(); } } System.err.println(text.getText1()); System.err.println(text.getText2()); printValue(text); } private static void printValue(Text text)...{ BeanInfo beanInfo = null; try ...{ beanInfo = Introspector.getBeanInfo(text.getClass()); } catch(IntrospectionException e) ...{ } PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); for(int i=0;i<descriptors.length;i++)...{ PropertyDescriptor pd = descriptors[i]; java.lang.reflect.Method tt = pd.getReadMethod(); try ...{ System.out.println(pd.getName()+": "+tt.invoke(text, new Object[]...{})); } catch(Exception ex) ...{ ex.printStackTrace(); } } }}