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

测试代码如下

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. postgresql 的 .pgpass密码文件的使用

    pgpass 是 连接 postgresql 时使用的密码文件,通常位置为 ~/.pgpass.在使用某些组件时还真的必须使用.具体的格式为: hostname:port:database:usern ...

  2. legend3---16、网站的安全性问题

    legend3---16.网站的安全性问题 一.总结 一句话总结: 通过客户端传递参数的方式也是有些危险,需要注意 单纯的获取数据的方法还好,但是 修改数据库方法一定要同时做前后端验证 1.php的b ...

  3. <JavaScript>调用apply报错:CreateListFromArrayLike called on non-object;

    Function.apply(obj, args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args--> ...

  4. angular父组件通过@ViewChild 主动获取子组 件的数据和方法

    1.调用子组件给子组件定义一个名称 <app-footer #footerChild></app-footer> 2. 引入 ViewChild import { Compon ...

  5. SSH工具登录远程指定节点时输入用户名密码方式【我】

    通过堡垒机部署项目, 一种方式:先把补丁传到接入机,然后用CRT等命令行工具登录指定接入机,然后用SCP命令把 补丁传到 生产服务器,比如 scp -P90010 /app/backup/packet ...

  6. Realsense D430 save

    rs-save-to-disk.cpp // License: Apache 2.0. See LICENSE file in root directory. // Copyright(c) 2015 ...

  7. python 对xls写入信息

    只能新创建xls # coding=utf-8import xlwt writebook = xlwt.Workbook()                #打开excel test= writebo ...

  8. PHP上传文件 Error 6解决方法 (转)

    按:我采用phpstudy2016,编辑php.ini ,“upload_tmp_dir没放开,直接放开,并指向 /tmp 就OK 上传文件,$_FILES["file"][&qu ...

  9. SpringMvc+ajax跨域请求时,出现options类型的请求并返回403的解决方案

    在使用 $.ajax({ url:'http://127.0.0.1:8081/rest/ccxxx/xxxx', type:'POST', dataType:"json", co ...

  10. Vue-cli安装,创建Vue项目

    1.安装脚手架: npm install -g vue-cli 2.查看vue: vue 3.查看可以使用的模板: vue list 4.创建项目sell: vue init webpack sell ...