1)第一种和第二种方式采用 Class 对象去加载,第三种方式采用 ClassLoader 对象去加载资源文件,之所以 Class 可以加载资源文件,是因为 Class 类封装的 ClassLoader 的 getResourceAsStream() 方法,从 Class 类中的源码可以看出:
public InputStream getResourceAsStream(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResourceAsStream(name);
return cl.getResourceAsStream(name);
理由:之所以这样做无疑还是方便客户端的调用,省的每次获取ClassLoader才能加载资源文件的麻烦!
2).class 是获取当前类的 class 对象,getClassLoader()是获取当前的类加载器,什么是类加载器?简单点说,就是用来加载java类的,类加载器就是负责把class文件加载进内存中,并创建一个java.lang.Class类的一个实例,也就是class对象,并且每个类的类加载器都不相同,getResourceAsStream(path)是用来获取资源的,因为这是ClassLoader(类加载器)获取资源,而类加载器默认是从 classPath 下获取资源的,因为这下面有class文件。所以这段代码总的意思是通过类加载器在 classPath 目录下获取资源,并且是以流的形式。我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,而且类加载器之间存在父子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),所以在这里通过当前类的加载器来加载资源也就是保证是和类类型是同一个加载器加载的。
(3)class.getClassLoader().getResourceAsStream() 和 class.getResouceAsStream() 的区别
a)class.getClassLoader().getResourceAsStream(String name) 默认从classpath中找文件(文件放在resources目录下),name不能带"/",否则会抛空指针。采用相对路径, "/"就相当于当前进程的根目录,即项目根目录;
inStream = PropertiesTest.class.getClassLoader().getResourceAsStream("com/test/demo/test.properties");
b)class.getResourceAsStream(String name) 是采用绝对路径,绝对路径是相对于 classpath 根目录的路径,"/" 就代表着 classpath,所以 name 属性需要前面加上 "/";
inStream = PropertiesTest.class.getResourceAsStream("/com/test/demo/test.properties")
方式二:采用Spring注解
如果工程中使用Spring,可以通过注解的方式获取配置信息,但需要将配置文件放到Spring配置文件中扫描后,才能将配置信息放入上下文。
<context:component-scan base-package="com.xxxx.service"/>
<context:property-placeholder location="classpath:properties/xxx.properties" ignore-unresolvable="true"/>
然后在程序中可以使用 @Value进行获取properties文件中的属性值,如下:
@Value("${xxxt.server}")
private static String serverUrl;
方式三:采用Spring配置
也可以在Spring配置文件中读取属性值,赋予类成员变量
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:properties/xxx.properties"/>
</bean>
<bean id="service" class="com.xxxx.service.ServiceImpl">
<property name="serverUrl" value="${xxxt.server}" />
</bean>
</beans>
重点:SpringBoot项目启动后,动态的读取类路径下文件数据
InputStream inputStream = EncryptUtil.class.getResourceAsStream("/HelloServiceEncryptFile.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = reader.readLine();
// 获取类路径下的文件路径
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists()) {
path = new File("");
log.info("path = {}", path.getAbsolutePath());
File upload = new File(path.getAbsolutePath(), "com/study/service");
if (!upload.exists()) {
upload.mkdirs();
FileOutputStream fos = new FileOutputStream(upload.getAbsolutePath() + File.separator + "hello.txt");
IoUtil.copy(inputStream, fos);
fos.close();
inputStream.close();
注意:此时我想读取 jar 包中根路径下的 HelloServiceEncryptFile.txt 文件,然后重新写入到根路径下的 com.study/service 路径下!
每天进步一点点,继续前进......