JavaM004—Properties的load方法

Properties继承了HashTable,HashTable实现了Map

一、Properties继承的源码如下:

Properties extends Hashtable<Object,Object>
 public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable

二、Properties的load方法的具体使用场景–加载数据库资源

1、DataSourceUtil 代码如下

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.test.BaseConfigUtil;


/**
 * 加载数据源类,将${catalina.home}/ybg/conf下面的所有数据源文件加入到bean工厂中
 */
public class DataSourceUtil {

    /**
     * 初始化
     * 
     * @param applicationContext
     */
    public static void init(ApplicationContext applicationContext) {

            File dataDirectory = new File(System.getProperty("catalina.home") + File.separator + "test" + File.separator + "conf");
    // 数据源文件必定要是properties类型。 
    //过滤的文件名称以db开头,以properties结尾
        File[] dataFiles = dataDirectory.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                String fileName = name.toLowerCase();

                if (fileName.endsWith(".properties")) {
                    if(fileName.startsWith("db")) {
                        return true;
                    }
                }

                return false;
            }
        });
    try (Reader reader = new InputStreamReader(new FileInputStream(dataFiles[i]), "UTF-8")) {
                Properties properties = new Properties();
                properties.load(reader);//读取数据库的.properties配置文件

                if (!properties.isEmpty()) {
                    // 建立数据源的bean
                    Map<String, Object> propertyMap = new HashMap<String, Object>();

                    propertyMap.put("driverClassName", properties.getProperty("sqlDriver"));
                    propertyMap.put("url",             properties.getProperty("sqlUrl"));
                    propertyMap.put("username",        properties.getProperty("sqlUsername"));
                    propertyMap.put("password",        properties.getProperty("sqlPassword"));
                    propertyMap.put("initialSize",     properties.getProperty("sqlInitialSize"));
                    propertyMap.put("maxTotal",        properties.getProperty("sqlMaxTotal"));
                    propertyMap.put("maxIdle",         properties.getProperty("sqlMaxIdle"));
                    propertyMap.put("maxWaitMillis",   properties.getProperty("sqlMaxWaitMillis"));

                    BeanUtil.addBean( "org.apache.commons.dbcp2.BasicDataSource", name + "_dataSource", propertyMap, null, null, null, "close");

                    // 建立jdbcTemplate的bean
                    List<String> referenceConstrList = new ArrayList<String>();
                    referenceConstrList.add(name + "_dataSource");

                    BeanUtil.addBean( "org.springframework.jdbc.core.JdbcTemplate", name + "_jdbcTemplate", null, null, null, referenceConstrList, null);
                    LogUtil.info("load datasource file {} success!", dataFiles[i].getAbsolutePath());
                }
            } catch (Exception e) {
                LogUtil.error("load datasource file {} fail!", dataFiles[i].getAbsolutePath());
            }
        }
}

    /**
     * 获取数据源

     * @param datasourceFileName 数据源配置文件名
     * @return
     */
    public static JdbcTemplate getJdbcTemplate(String datasourceFileName) {
        String id = datasourceFileName + "_jdbcTemplate";
        return BeanUtil.getBean(id, JdbcTemplate.class);
    }
}

2、BeanUtil代码如下

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;

/**
 * 管理bean类
 */
public class BeanUtil {

    // Spring Bean 工厂
    public static DefaultListableBeanFactory beanFactory;
    public static void init(ApplicationContext applicationContext) {
        beanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
    }
    /**
     * 动态增加bean
     * 
     * @param className
     * @param beanKey
     * @param propertyMap
     * @param referenceMap
     * @return
     */
    public static boolean addBean(
            String className, String beanKey, Map<String, Object> propertyMap,
            Map<String, String> referenceMap, List<Object> propertyConstrList,
            List<String> referenceConstrList, String destroyMethod) {
        // 判断Spring容器中是否存在该Bean
        if (beanFactory.containsBeanDefinition(className)) {
            return true;
        }

        BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(className);
        bdb.getBeanDefinition().setAttribute("id", beanKey);

        // 设置bean参数属性
        if (propertyMap != null && !propertyMap.isEmpty()) {
            Iterator<String> propertyKeys = propertyMap.keySet().iterator();
            while (propertyKeys.hasNext()) {
                String keyString = propertyKeys.next();
                bdb.addPropertyValue(keyString, propertyMap.get(keyString));
            }
        }

        // 设置bean参数引用
        if (referenceMap != null && !referenceMap.isEmpty()) {
            Iterator<String> referenceKeys = referenceMap.keySet().iterator();
            while (referenceKeys.hasNext()) {
                String keyString = referenceKeys.next();
                bdb.addPropertyReference(keyString, referenceMap.get(keyString));
            }
        }

        // 设置bean构造参数属性
        if (propertyConstrList != null && !propertyConstrList.isEmpty()) {
            for (int i = 0; i < propertyConstrList.size(); i++) {
                bdb.addConstructorArgValue(propertyConstrList.get(i));
            }
        }

        // 设置bean构造参数引用
        if (referenceConstrList != null && !referenceConstrList.isEmpty()) {
            for (int i = 0; i < referenceConstrList.size(); i++) {
                bdb.addConstructorArgReference(referenceConstrList.get(i));
            }
        }

        if (destroyMethod != null && !destroyMethod.isEmpty()) {
            bdb.setDestroyMethodName(destroyMethod);
        }

        beanFactory.registerBeanDefinition(beanKey, bdb.getBeanDefinition());

        return true;
    }

    /**
     * 获取Bean
     * 
     * @param requiredType
     * @return
     */
    public static <T> T getBean(Class<T> requiredType) {
        return beanFactory.getBean(requiredType);
    }

    /**
     * 获取bean
     * @param beanName
     * @param requiredType
     * @return
     */
    public static <T> T getBean(String beanName,Class<T> requiredType) {
        return  beanFactory.getBean(beanName,requiredType);
    }
}

转自https://blog.csdn.net/u013592116/article/details/73181423

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值