Properties的load方法的具体使用场景--加载数据库资源
来源:互联网 发布:淘宝分享有赏 编辑:程序博客网 时间:2024/06/06 05:50
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); }}
(如有错误,欢迎纠错)
阅读全文
0 0
- Properties的load方法的具体使用场景--加载数据库资源
- 使用Properties的load方法加载属性信息
- 【Properties】Properties的load方法
- $(window).load()方法的使用场景
- Properties的load方法加载的文件格式要求
- java.util.Properties类的load()方法
- Redis缓存的具体使用场景
- 加载properties文件的方法
- jQuery使用ajax的load()加载页面方法
- unity动态加载场景Resources.Load方法
- Properties的store()方法和load()方法 示例
- Properties的store()方法和load()方法 示例
- 在使用Jquery的load方法加载时,只加载第一次的问题解决方法
- 读取资源文件的方法 ApplicationResources.properties
- ViewStub的具体使用场景,不是万能的
- java编程中Properties类的具体作用和使用
- Struts资源文件(application.properties)的使用
- 图片等资源的使用Properties.Resources
- Filter过滤登录权限 不过滤登录页面 及死循环问题解决
- Oracle 在视图中使用变量
- [绍棠] UIPageViewController多页面混合开发
- 常用表单输入正则表达式(数字,字符,时间验证等)
- 微信小程序开发(六)小程序支付-notify_url
- Properties的load方法的具体使用场景--加载数据库资源
- Java学习笔记——循环
- Android静态变量的生命周期
- mysql的慢日志
- Core ML 与 Vision:iOS 11 机器学习教程
- Qt学习02——容器类
- PHP-开发环境搭建
- 随笔
- mysql中int、bigint、smallint 和 tinyint的区别详细介绍