首先声明一点,springboot获取资源文件,需要看是

  1》从spring boot默认的application.properties资源文件中获取

  2》还是从自定义的资源文件中获取

带着这个想法去看下面几种方式

===============================================================================================

1》从spring boot默认的application.properties资源文件中获取

先给出来application.properties的内容

#方式1
com.sxd.type1 = type1
com.sxd.title1 = 使用@ConfigurationProperties获取配置文件#方式2
com.sxd.type2 = type2
com.sxd.title2 = 使用@Value获取配置文件#方式3
com.sxd.type3 = type3
com.sxd.title3 = 使用Environment获取资源文件#map
com.sxd.login[username] = sxd
com.sxd.login[password] = admin123
com.sxd.login[callback] = http://www.cnblogs.com/sxdcgaq8080/#list
com.sxd.comList[0] = com1
com.sxd.comList[1] = com2
com.sxd.comList[2] = com3

View Code

①===第一种方式:使用@ConfigurationProperties获取配置文件

先搞一个绑定资源文件的bean

注意属性名和资源文件中的属性名相一致。

package com.sxd.beans;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Component
@ConfigurationProperties(prefix = "com.sxd")
//@PropertySource("classpath:/application.properties")
//不用这个注解,默认就是加载application.properties资源文件
public class User {private String type1;private String title1;private Map<String,String> login = new HashMap<>();private List<String> comList = new ArrayList<>();public String getType1() {return type1;}public void setType1(String type1) {this.type1 = type1;}public String getTitle1() {return title1;}public void setTitle1(String title1) {this.title1 = title1;}public Map<String, String> getLogin() {return login;}public void setLogin(Map<String, String> login) {this.login = login;}public List<String> getComList() {return comList;}public void setComList(List<String> comList) {this.comList = comList;}
}

View Code

然后在启动类中使用

package com.sxd.secondemo;import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@SpringBootApplication
@EnableConfigurationProperties(User.class)
public class SecondemoApplication {@AutowiredUser user;@RequestMapping("/")public String hello(){user.getLogin().forEach((k,v)->{System.out.println("map的键:"+k+">>map的值:"+v);});user.getComList().forEach(i->{System.out.println("list的值:"+i);});return user.getType1()+user.getTitle1();}public static void main(String[] args) {SpringApplication.run(SecondemoApplication.class, args);}
}

View Code

结果如下:

控制台打印:

访问地址:

②===第二种方式:使用@Value获取配置文件

这里不用搞一个绑定资源文件的bean了。

只需要在你想用的地方使用@Value调用你想要的属性名对应的值即可。

package com.sxd.secondemo;import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@SpringBootApplication
public class SecondemoApplication {@Value("${com.sxd.type2}")private String type;@Value("${com.sxd.title2}")private String title;@RequestMapping("/")public String hello(){return type+title;}public static void main(String[] args) {SpringApplication.run(SecondemoApplication.class, args);}
}

View Code

访问结果:

③===第三种方式:使用Environment获取资源文件

也不用提前做什么使用,Environment就是一个全局的资源池,application.properties中的属性值都可以从这里获取到。

package com.sxd.secondemo;import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@SpringBootApplication
public class SecondemoApplication {@AutowiredEnvironment environment;@RequestMapping("/")public String hello(){return environment.getProperty("com.sxd.type3")+environment.getProperty("com.sxd.title3");}public static void main(String[] args) {SpringApplication.run(SecondemoApplication.class, args);}
}

View Code

运行结果:

================================================================================================

2》从自定义的资源文件中获取属性值

①===第一种方式:使用@ConfigurationProperties获取配置文件

自定义资源文件如下:

然后指定绑定自定义资源文件

package com.sxd.beans;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "com.sxd")
@PropertySource("classpath:/test.properties")
//需要用这个注解,默认就是加载application.properties资源文件,替换@ConfigurationProperties取消location属性的效果
public class User {private String type1;private String title1;public String getType1() {return type1;}public void setType1(String type1) {this.type1 = type1;}public String getTitle1() {return title1;}public void setTitle1(String title1) {this.title1 = title1;}}

View Code

最后在启动类中使用一下

package com.sxd.secondemo;import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@SpringBootApplication
@EnableConfigurationProperties(User.class)
public class SecondemoApplication {@AutowiredUser user;@RequestMapping("/")public String hello(){return user.getType1()+user.getTitle1();}public static void main(String[] args) {SpringApplication.run(SecondemoApplication.class, args);}
}

View Code

运行结果:

②===第二种方式:使用@Value获取配置文件

先设定一个自定义资源文件

如下,自定义资源文件需要满足application-{profile}.properties格式

然后在application.properties文件中指明加载这个资源文件

spring.profiles.active=test
#spring.profiles.include=test

这两种哪种都可以加载上自定义的资源文件,后面的test就是上面{profile}的值

最后在启动类中使用@Value获取自定义资源文件中的属性,这个时候自定义的资源文件已经在application,properties文件中被指明要被加载了,因此是可以被获取到的

package com.sxd.secondemo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@SpringBootApplication
public class SecondemoApplication {@Value("${com.sxd.type2}")private String type;@Value("${com.sxd.title2}")private String title;@RequestMapping("/")public String hello(){return type+title;}public static void main(String[] args) {SpringApplication.run(SecondemoApplication.class, args);}
}

View Code

运行结果:

③===第三种方式:使用Environment获取资源文件

首先还是写一个自定义的资源文件,文件命名同上面第二种方式一样

接着,在application.properties中声明加载这个自定义的资源文件

最后在启动类中,也就是哪里使用就在那里自动注入Environment.

package com.sxd.secondemo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@SpringBootApplication
public class SecondemoApplication {@AutowiredEnvironment environment;@RequestMapping("/")public String hello(){return environment.getProperty("com.sxd.type3")+environment.getProperty("com.sxd.title3");}public static void main(String[] args) {SpringApplication.run(SecondemoApplication.class, args);}
}

View Code

运行结果:

==================================================================================================================

===================================================完成============================================================

【spring Boot】spring boot获取资源文件的三种方式【两种情况下】相关推荐

  1. java获取资源文件的各种方法

    1.在test环境中获取xml: @Test     public void testFindUserById() throws Exception{ String resource = " ...

  2. java 获取文件版本号_Java 获取资源文件路径

    1 问题描述 通过源码运行时,一般使用如下方式读取资源文件: String str = "1.jpg"; 资源文件与源码文件放在同一目录下,或者拥有同一父级目录: String s ...

  3. java get image获取根路径_Java 获取资源文件路径

    1 问题描述 通过源码运行时,一般使用如下方式读取资源文件: String str = "1.jpg"; 资源文件与源码文件放在同一目录下,或者拥有同一父级目录: String s ...

  4. getresourceasstream 路径_Java 获取资源文件路径

    1 问题描述 通过源码运行时,一般使用如下方式读取资源文件: String str = "1.jpg"; 资源文件与源码文件放在同一目录下,或者拥有同一父级目录: String s ...

  5. java资源文件路径_Java 中获取资源(文件)的路径问题总结

    Java 中获取资源(文件)的路径问题总结 首先,Java 中获取资源大体上可分为两种方式,基于 文件系统的 和 基于classpath的. 1. 基于文件系统的相对简单. 比如 构造一个File f ...

  6. python获取绝对路径_python3中获取文件当前绝对路径的两种方法

    方法1: import sys print(sys.argv) 得到文件当前绝对路径字符串的一个列表 ['D:/pycharm/PracticeProject/ClientServerNetworki ...

  7. php保存流文件到本地,php下载保存文件保存到本地的两种实现方法

    第一种:<?php function downfile() { $filename=realpath("resume.html"); //文件名 $date=date(&qu ...

  8. PPT文件设置打开密码的两种方法

    PPT文件设置了打开密码加密,可以保护重要文件内容,今天介绍两种加密PPT文件的方法: 第一种: 点击工具栏中的[文件] 然后点击[信息]-[保护演示文稿]-[用密码进行加密] 然后输入密码,就完成了 ...

  9. 让Mac复制文件到U盘的两种方法

    2019独角兽企业重金招聘Python工程师标准>>> 让Mac复制文件到U盘的两种方法 一.让Mac支持ntfs格式磁盘 下载安装tuxera ntfs这款Mac读写软件,然后在系 ...

  10. 【Spring源码】ClassPathResource 资源文件源码分析

    上一篇文章我们主要介绍了开发 Spring 应用涉及到的一些核心组件,在文章的最后搭建了开发环境.那么接下来我们开始分析 Spring 源码部分,本篇文章首先分析 Spring 是如何封装资源文件的. ...

最新文章

  1. Galaxy Release 20.05 发布,新增多项可视化体验
  2. redis-5.0.4集群部署
  3. P4124 [CQOI2016]手机号码
  4. 关于VS打包程序无法弹出主界面的问题
  5. 为什么要将表和索引建立在不同的表空间?
  6. 小朋友学C++(1)
  7. 扫地机器人划伤地板_扫地机器人哪个牌子好?会选的才能买到好产品
  8. WPF使用Webbrowser操作网页的主要代码
  9. 后处理曲线编辑_LSPREPOST后处理
  10. 如何学习工业机器人技术
  11. 5、Hive数据仓库——Hive分区及动态分区
  12. VS2017生成可执行程序,执行提示“不是有效的win32应用程序”
  13. 下面有9个点的图片。你能一笔画出4条直线连接着9个点,并且不重复任何一条线吗?
  14. 【论文阅读】Rotom: A Meta-Learned Data Augmentation Framework for Entity Matching
  15. ubuntu拷贝和移动文件和文件夹
  16. 利用ICMP协议,使用python原始套接字实现主机存活探测工具
  17. Android 沉浸式状态栏攻略 让你的状态栏变色吧
  18. 基于html5的在线教育平台的设计与实现,网络在线教学系统设计与实现.doc
  19. 专访钟家鸣:开源锁仓,IOST要做什么?
  20. 碎片文字摘录,触动心灵的文字,愿其成为我们前行的慰藉

热门文章

  1. 一条 update 语句引起的事故,这回可以长长记性了
  2. 一线城市的繁荣vs年轻人的梦想?
  3. c#服务器上的文件怎么打印机,如何通过使用C#窗口服务通过打印机打印数据打印文本文件...
  4. php远程调试 没有光标,老法师谈技术 - PHP远程单步调试
  5. mysql backup 使用_mysqlbackup (官方使用)
  6. python回溯算法全排列_从全排列看回溯算法
  7. python判断丑数_LeetCode-python 264.丑数 II
  8. 计算机系统操作工中级工试卷,计算机系统操作工中级理论试题及答案.doc
  9. pytorch标签onehot编码_pytorch将标签转为onehot
  10. js 右键菜单_Windows下的多场景「快捷菜单」工具箱