码迷,mamicode.com
首页 > 编程语言 > 详细

java----内省

时间:2019-04-22 22:32:21      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:int   1.5   java   api   geturl   20px   tar   设置   rgs   

 内省:

通过内省Api来装配一个bean对象,bean对象的值是通过配置文件来获取

即通过配置文件中的属性和特定的类信息来,来组装一个对象,便于维护;

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 Demo {
    public static void main(String[] args) {
        try {
            Config bean = ((Config) BeanFactory.getBean("bean.name"));
            System.out.println(bean);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
}
class Config{
    private String username;
    private String password;
    private String 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;
    }
    @Override
    public String toString() {
        return "Config{" +
                "username=‘" + username + ‘\‘‘ +
                ", password=‘" + password + ‘\‘‘ +
                ", url=‘" + url + ‘\‘‘ +
                ‘}‘;
    }
}
class BeanFactory{
    private static Properties p = new Properties();
    //使用静态代码块读取配置文件
    static {
        InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("com\\first\\conf.properties");
        try {
            p.load(inStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static Object getBean(String name) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        String beanName = p.getProperty(name);
        //获取类信息
        Class<?> aClass = Class.forName(beanName);
        //实例化一个bean对象
        Object bean = aClass.newInstance();
        try {
            //通过类信息,获取javaBean的描述信息
            BeanInfo beanInfo = Introspector.getBeanInfo(aClass);
            //通过描述信息,获取该类的所有属性描述器,该类必须将属性字段设置get和set方法;(才能获取)
            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,p.getProperty("user.name"));
                }else if("password".equals(propertyName)){
                    writeMethod.invoke(bean,p.getProperty("user.password"));
                }else if("url".equals(propertyName)){
                    writeMethod.invoke(bean,p.getProperty("url"));
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return bean;
    }
}

配置文件   com\first\conf.properties

bean.name=com.first.Config
user.name=admin
user.password=admin
url=http://www.baidu.com

  

java----内省

标签:int   1.5   java   api   geturl   20px   tar   设置   rgs   

原文地址:https://www.cnblogs.com/yanxiaoge/p/10753346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!