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

测试代码如下

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. java之map遍历

    java开发中常常会用到遍历,所以下边就列举四种map的遍历方法. public class testMap { public static void main(String[] args) { Ma ...

  2. 判断 js 类型的方式

    1. typeof 可以判断出'string','number','boolean','undefined','symbol'但判断 typeof(null) 时值为 'object'; 判断数组和对 ...

  3. 小程序checkbox调整大小

    .cb{ transform: scale(0.6,0.6); } <view> <label class="lab" for="box1"& ...

  4. P5662 纪念品

    P5662 纪念品 题解 拿到题目想到DP,但是就是不知道咋写 后来证实这是个背包DP(最近整理背包白整了 我们观察这道题目的特殊之处: 也就是说,对于手中的物品,我们可以今天买了然后明天早上接着卖出 ...

  5. nginx里面的location 规则匹配

    nginx location语法 ~ # 区分大小写的正则匹配 location ~ \.(gif|jpg|png|js|css)$ { #规则D } ~* # 不区分大小写的正则匹配(和~的功能相同 ...

  6. Angular 中的数据交互(get jsonp post)

    一.Angular get 请求数据 Angular5.x 以后 get.post 和和服务器交互使用的是 HttpClientModule 模块. 1.在 app.module.ts 中引入 并注入 ...

  7. osg qt ifc

    ui_ifcproject_20190702.h #pragma once /************************************************************* ...

  8. Spring MVC Action参数类型 List集合类型(简单案例)

    题目:定义一个员工实体(Employee),实现批量添加员工功能,在表单中可以一次添加多个员工,数据可以不持久化 1,新建一个项目 2, 然后选择Maven框架选择 maven-archetype-w ...

  9. LVS搭建负载均衡集群(一)——NAT模式

    (1).集群技术的分类 集群技术主要分为三大类:负载均衡(Load Balance)集群,简称LB集群:高可用(High Availability)集群,简称 HA 集群:高性能计算(High Per ...

  10. 我的iOS动画01

    1.嵌套使用,先变大再消失 [UIView animateWithDuration:1.25 aniamtions:^{ CGAffineTransform newTRansform = CGAffi ...