在项目中我们经常会需要读一些配置文件来获取配置信息,然而对于这些配置文件在项目中存放的位置以及获取这些配置文件的存放路径却经常搞不清楚,自己研究了一下,记录下来以备后用。

测试代码如下

package com.example.test.aspect;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties; public class Main { public static void main(String[] args) throws Exception{
System.out.println(Main.class.getResource("/application.properties").getPath());
System.out.println(Main.class.getResource("").getPath());
System.out.println(Main.class.getClassLoader().getResource("").getPath());
System.out.println(ResourceUtils.getFile("classpath:application.properties")); File file = new File(Main.class.getClassLoader().getResource("application.properties").getPath());
InputStream inputStream = new FileInputStream(file);
Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.getProperty("sdf")); } }

  1、Main.class.getResource("/").getPath()) , 获取的是项目的类路径,对于springboot项目resource目录下的文件编译后会放在类路径下

  2、Main.class.getResource("").getPath(),获取的则是当前类Main.classs所在包的路径

  3、Main.class.getClassLoader().getResource("").getPath(),通过类加载器获取的是项目的类路径

  4、还可以使用spring的工具类利用ResourceUtils.getFile("classpath:application.properties")获取resource目录下的配置文件

  5、最后借助于jdk properties读取配置文件中的信息

  代码运行结果如下:

关于spring中配置文件路径的那些事儿的更多相关文章

  1. 小成就之解决调用spring中FileSystemXmlApplicationContext路径问题

    此文写下调用spring过程中遇到的一个问题!或许对于入行的人一看觉得我很傻逼吧,这问题谁都会了!但我觉得对于新手(自已)来说,算是一个好思路与好办法! 问题: 对于 test_aa ta = (te ...

  2. Java中如何获取spring中配置文件.properties中属性值

    通过spring配置properties文件 1 2 3 4 5 6 7 8 9 <bean id="propertyConfigurer"   class="co ...

  3. vue2.0中配置文件路径

    在build/webpack.base.conf.js中添加一些代码即可 module.exports = { resolve: { extensions: ['.js', '.vue', '.jso ...

  4. Spring中配置文件中引用外部文件

    src\dayday\conn.java package dayday;import java.sql.Connection;import java.sql.DriverManager;import ...

  5. hibernate下载包中配置文件路径

    路径:hibernate-release-5.0.2.Final\project\hibernate-ehcache\src\test\resources\hibernate-config 文件:hi ...

  6. Spring中配置文件applicationContext.xml配置详解

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  7. (二)Spring中的ioc

    目录 Spring的ioc操作 ioc底层使用的技术 ioc底层原理 ioc入门案例 bean管理(xml方式) IOC和DI的区别 Spring整合WEB的原理 Spring 整合的具体操作 Spr ...

  8. Spring中多配置文件以及寻觅引用其他bean的方式

    Spring多配置文件有什么好处? 按照目的.功能去拆分配置文件,可以提高配置文件的可读性与维护性,如将配置事务管理.数据源等少改动的配置与配置bean单独分开. Spring读取配置文件的几种方式: ...

  9. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

随机推荐

  1. oneway modifier MQ 发送请求不接受任何响应

    Apache Thrift - Home http://thrift.apache.org/ /** * This method has a oneway modifier. That means t ...

  2. 对请求数据的格式化 方案 Spring Cloud Gateway features:

    对请求数据的格式化 例如 {body:{}}--->{data:{}} 执行阶段概念 · OpenResty最佳实践 · 看云 https://www.kancloud.cn/kancloud/ ...

  3. python 设计模式之状态模式

    1.为什么会出现状态模式? 在软件开发过程中,各种应用程序可能会根据不同的情况做出不同的处理.最直接的方案就是把所有的可能发生的情况都考虑到.然后使用条件语句(if...elseif...elseif ...

  4. python 适合的才是最好的

    群里老有人问最新的破解码最新的包,最新的就是最好的吗? 今天说一下这些新手的坑: numpy 最好使用的版本是1.13.3  而非新的1.17.0 pandas最好使用的版本是0.18.0 而非新的0 ...

  5. 放射渐变RadialGradient

    public RadialGradient(float centerX, float centerY, float radius, int[] colors, float[] stops, TileM ...

  6. python方法未绑定错误

    相信 Python 程序员多多少少都和我一样遇到过 Method Unbound Error,直译过来就是 “方法未绑定错误”,虽然搜索之后知道了使用 @classmethod 这样的装饰起后就可以解 ...

  7. Windows下安装ActiveMQ

    到官网(http://activemq.apache.org/download-archives.html)下载最新发布的压缩包(我下的是5.15.9)到本地后解压(我解压到D盘Dev目录下)即可.进 ...

  8. 不是springboot项目怎么使用内置tomcat

    不是springboot项目怎么使用内置tomcat   解决方法: 1.pom.xml中添加以下依赖 <properties>  <tomcat.version>8.5.23 ...

  9. iOS 越狱后OpenSSH安装报错

    转载自:https://www.jianshu.com/p/75f6d0f54d61 安装OpenSSH报错 尝试重启手机重新安装 将手机语言设置为英文状态 将手机设置为飞行模式,用wifi下载(我的 ...

  10. iOS compile sources问题

    以前xcode compile sources 这里是自动生成的,升级到新版本之后,我删掉这里的列表,就没法生成了.导致项目无法运行. 解决方法就是:自己手动添加.h文件