❀ 通过类加载器进行加载,通过流的方式进行读取,从而获取资源
1、java的jdk内置的加载方式:
通过线程Thread(lang包的)获取到类加载,然后以流的方式获取资源:
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
/* 【使用输入流inputStream】 */
private static Properties p = new Properties();
//加载注册
static {
try {
InputStream inStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("db.properties");
p.load(inStream);
Class.forName(p.getProperty("driverClassName"));//获取driverClassName的值
} catch (Exception e) {
e.printStackTrace();
}
}
2、mybatis的的加载方式(通过):
通过资源对象Resources(import
org.apache.ibatis.io.Resources;)以流的方式获取资源:
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
/* 【使用输入流inputStream】 */
InputStream in