Some doubts about properties

Project scenario:

提示:刚开始学习spring时,运用properties的疑惑:

learn springioc


Problem Description

Past review:

Following the video, I wrote a bean factory.

When writing the code in the method later, after obtaining the key through properties, it will not work to obtain the value based on the obtained key. The result is null.

But by directly entering the key, it is possible to get the value.

Output result: 

 Code:

package com.springioc.factory;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
import java.util.Set;

public class BeanFactory {

    /**
     * 老师的方法:创建propertics对象,再创建一个静态代码块,通过反射获取到factory.properties
     * 通过反射获取到factory.properties之后,再创建getDao()方法,通过properties的方法获取到里面的value
     * 通过读取到的value(全类名)通过反射创建对象。进行调用
     *
     * @return
     */


    private static Properties properties;
//  使用配置文件实例化  properties 对象
//    Properties properties = new Properties();
    static {
        properties = new Properties();
        /*
        1,获取到resouse中的路径??,路径是啥???
        通过反射获取配置文件??(不用),直接获取读取配置文件????()
        通过反射拿到 classLoder字节流,
         */
//        properties.load(new FileInputStream("resources/factory.properties"));
        InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("factory.properties");
    try {
        properties.load(is);
    } catch (IOException e) {
        e.printStackTrace();
    }

}
//    private Object getDao = new HelloDaoImpl1();
//    是创建方法,不引用对象了????
    public static Object getDao() {
        Set keySet = properties.keySet();
        System.out.println("keySet:"+keySet);
//            获取到properties中的值,也就是 HellDaoimpl中的 接口实现类的路径
        String lujing = properties.getProperty(String.valueOf(keySet));
        System.out.println("keysettostring:" + keySet.toString());
        System.out.println("HelloDao的路径:" + lujing);


        String value = properties.getProperty("helloDao");  //得到value这个全类名,通过反射创建对象
        System.out.println("value:" + value);


        try {
//            clazz = Class.forName(lujing);
            Class clazz = Class.forName(value);
            Object object = clazz.getConstructor(null).newInstance(null);
            return object;


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


//            抛异常:选中,ctrl+alt+t
//            根据路径创建 HelloDao 对象??
//            反射???,获取类路径-》创建对象



//        return new HelloDaoImpl1();
//        return properties.getProperty(keySet.toString());
        return null;
    }

}


Cause Analysis:


solution:

Guess you like

Origin blog.csdn.net/m0_58115520/article/details/130306714