经典的JAVA BEAN处理

 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();
           }

       }

   }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值