方案是将properties文件放在src/main/resoure的目录下
然后在使用类加载器将properties文件流对象加载为properties对象
InputStream isp = getClass().getResourceAsStream("/Druid.properties"); properties.load(isp);
示例代码如下:
@Test public void DruidTest1() throws Exception { Properties properties = new Properties(); InputStream isp = getClass().getResourceAsStream("/Druid.properties"); properties.load(isp); DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); Connection connection = dataSource.getConnection(); connection.close(); }
//
不知道为啥静态代码块和加注释的测试方法中加载同一路径下的properties文件路径不同
在MAIN方法中代码如下:
Properties properties=new Properties(); InputStream ips = JdbcUtils.class.getClassLoader().getResourceAsStream("Druid.properties");