java----内省

Posted 小名的同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java----内省相关的知识,希望对你有一定的参考价值。

 内省:

通过内省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;
    }
}

配置文件   comfirstconf.properties

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

  

以上是关于java----内省的主要内容,如果未能解决你的问题,请参考以下文章

java内省小记

理解Java的反射与内省及其区别

java 中反射机制和内省机制的区别是啥?

java----内省

Java内省详解

java学习之内省