JAVA内省

基本概念

java语言对Bean类属性、事件的另外一种处理方法
一般来说说,其他类调用或者修改Bean对象的属性,应该使用构造方法,getName,setName之类的方式来改变其值,而内省是另一种方式

通过配置文件去修改bean属性,提高维护性

通过类Introspector调用getBeanInfo方法获取BeanInfo信息,然后通过BeanInfo来获取属性描述器PropertyDescriptor,再通过描述器获取getter/setter方法。

相关API

  1. Introspector类:
  2. BeanInfo类
  3. PropertyDescriptor类
    getReadMethod()
    getWriteMethod()
  4. MethodDescriptor类
    getMethod()

源码

config.java

public class config {
    private String name;
    private String password;
    public config() {
    }
    public config(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "config{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

configFactory.java

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class configFactory {
    private static Properties prop = new Properties();

    static {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
        try {
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Object getBean(String name){
        Object bean = null;
        String beanName = prop.getProperty(name);
        try {
            Class<?> aClass = Class.forName(beanName);
            bean = aClass.newInstance();

            BeanInfo beanInfo = Introspector.getBeanInfo(aClass);
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for(int i = 0;i<propertyDescriptors.length;i++){
                Method writeMethod = propertyDescriptors[i].getWriteMethod();
                String currentName = propertyDescriptors[i].getName();
                if (currentName.equals("name")){
                    writeMethod.invoke(bean,prop.getProperty("bean.name"));
                }else if(currentName.equals("password")){
                    writeMethod.invoke(bean,prop.getProperty("bean.password"));
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    return bean;
    }
}

config.properties

bean.classname=config
bean.name=Liu
bean.password=123

Main.java

public class Main {

    public static void main(String[] args) {
        config bean = (config) configFactory.getBean("bean.classname");
        System.out.println(bean);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值