内省(Introspector)

通过内省的API装配一个Bean对象,Bean对象的值是通过配置文件获取的,这样做可以提高代码的维护性

创建一个Config类

package com.vince.introspector;

public class Config {
    private String username;
    private String password;
    private String url;

    public Config() {
    }

    @Override
    public String toString() {
        return "Config{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", url='" + url + '\'' +
                '}';
    }

    public Config(String username, String password, String url) {
        this.username = username;
        this.password = password;
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

创建一个生产Bean的工厂类

package com.vince.introspector;

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 BeanFactory {
    private static Properties pro = new Properties();
    //使用静态代码块读取配置文件
    static{
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/vince/introspector/config.properties");
        try {
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static Object getbean(String name){
        Object bean = null;
        String property = pro.getProperty(name);
        try {
            Class<?> aClass = Class.forName(property);
            bean = aClass.newInstance();
            //通过类信息获取javabean的描述信息
            BeanInfo beanInfo = Introspector.getBeanInfo(aClass);
            //通过javabean的描述信息,获取该类的所有属性信息
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                String propertyname = propertyDescriptors[i].getName();
                Method writeMethod = propertyDescriptors[i].getWriteMethod();
                if("username".equals(propertyname)){
                    //调用属性的set方法
                    writeMethod.invoke(bean,pro.getProperty("bean.username"));
                }else if("password".equals(propertyname)){
                    writeMethod.invoke(bean,pro.getProperty("bean.password"));
                }else if("url".equals(propertyname)){
                    writeMethod.invoke(bean,pro.getProperty("bean.url"));
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return bean;
    }

}

创建一个配置文件

bean.username=admin;
bean.password=123;
bean.url=http://baidu.com
bean.name=com.vince.introspector.Config
测试类
package com.vince.introspector;


import org.junit.Test;

/**
 * 通过内省的API来装配一个Bean对象,Bean对象的值是通过配置文件来获取的,目的是为了提高代码的维护性
 */
public class BeanFactoryTest {
    @Test
    public void test(){
        Config getbean = (Config) BeanFactory.getbean("bean.name");
        System.out.println(getbean);
    }
}
运行结果


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值