首先声明一点,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

①===第一种方式:使用@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;
}
}

然后在启动类中使用

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 { @Autowired
User 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);
}
}

结果如下:

控制台打印:

访问地址:

②===第二种方式:使用@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);
}
}

访问结果:

③===第三种方式:使用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 { @Autowired
Environment 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);
}
}

运行结果:

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

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;
} }

最后在启动类中使用一下

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 { @Autowired
User user; @RequestMapping("/")
public String hello(){
return user.getType1()+user.getTitle1();
} public static void main(String[] args) {
SpringApplication.run(SecondemoApplication.class, args);
}
}

运行结果:

②===第二种方式:使用@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);
}
}

运行结果:

③===第三种方式:使用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 { @Autowired
Environment 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);
}
}

运行结果:

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

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

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

  1. Spring Boot中的静态资源文件

    Spring Boot中的静态资源文件 1.SSM中的配置 2.Spring Boot 中的配置 2.1 整体规划 2.2 源码解读 2.3 自定义配置 2.3.1 application.prope ...

  2. 【Spring】获取资源文件+从File+从InputStream对象获取正文数据

    1.获取资源文件或者获取文本文件等,可以通过Spring的Resource的方式获取 2.仅有File对象即可获取正文数据 3.仅有InputStream即可获取正文数据 package com.sx ...

  3. wpf 前台获取资源文件路径问题

    1 <ImageBrush ImageSource="YT.CM.CommonUI;component/Resource/FloadwindowImage/middle.png&quo ...

  4. JavaWeb基础: 获取资源文件

    Web工程在编译构建完毕以后,需要部署到Tomcat上运行,资源的硬盘路径也会随着改变.要想对资源文件进行读写操作需要获取其硬盘地址,在Web工程中通常通过ServletContext/ClassLo ...

  5. java基础知识3--如何获取资源文件(Java中获取资源文件的url)

    java开发中,常见的resource文件有:.xml,.properties,.txt文件等,后台开发中经常用到读取资源文件,处理业务逻辑,然后返回结果. 获取资源文件的方法说明getResourc ...

  6. 关于获取资源文件,Class.getResource和ClassLoader.getResource的区别

    原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2019-02-22/29.html 作者:夜月归途 出处:http://www.guitu ...

  7. Java中获取资源文件的方法总结

    这里总结3中方法获取资源文件的 ServletContext Class ClassLoader 文件的位置 1. ServletContext public void doGet(HttpServl ...

  8. Java 获取资源文件路径

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

  9. 对比MFC资源文件谈谈WPF布局方式

    对比MFC资源文件谈谈WPF布局方式 MFC方式 对于传统的MFC基于UI的应用程序设计通常分两步走,首先是设计UI,使用的是RC文件,然后是代码文件,对RC文件进行操作,如下面Figure 1 的基 ...

随机推荐

  1. 预处理、const、static与sizeof-为什么不把所有的函数都定义成内联函数

    1:内联是以代码膨胀(复制)为代价的,仅仅省去了函数调用的开销,从而提高函数的执行效率.如果执行函数体内代码的时间相比于函数调用的开销较大,那么效率的收获会很小.另一方面,每一处内联函数的调用都要复制 ...

  2. Ubuntu桌面版与服务器版的区别(转)

    Ubuntu桌面版vs服务器版 提到安装Linux,Ubuntu可谓是最受欢迎的.为了满足每个人的需求,出现了不少版本或风格的Ubuntu:其中两项便是桌面版与服务器版.只要发布版本号一致,这两者从核 ...

  3. 带有时间间隔的dp

    Uberwatch 题意:一个人打一群敌人,每间隔时间m能释放一次大招,消灭这个时刻上的所有敌人,起始时刻开始计算冷却时间 solution: dp[i]=max(dp[i],dp[i-m]); /* ...

  4. Python 生成随机数函数和加密函数(MD5)

    内容来自debugtalk import hashlib import random import string def gen_random_string(str_len): '''生成指定长度的随 ...

  5. 记录一次webpack3升级到webpack4过程

    升级之前也参考了一些网上的教程.借鉴之,进行的自己的升级.一些版本为什么设为那个版本号也是参考别人的结果. 整体是按照先升级npm run dev:在升级npm run build的顺序. 首先升级w ...

  6. 阿里云轻应用服务器配置Ubuntu的JDK、Tmocat、Mysql和Redis

    1.与服务器建立连接(达到效果:XShell和Xftp均可连接到服务器)   阿里云管理控制台提供的三种建立服务器连接方式: 使用浏览器发起安全连接(推荐) 客户端使用密钥进行连接 客户端使用账号密码 ...

  7. MATLAB学习(七)求解优化问题:线性规划 非线性规划 拟合与插值 多目标规划

    Minf(x)=-5x1  -4x2  -6x3                x1   -x2    +x3  <=20              3x1  +2x2 +4x3 <=42 ...

  8. kNN进邻算法

    一.算法概述 (1)采用测量不同特征值之间的距离方法进行分类 优点: 精度高.对异常值不敏感.无数据输入假定. 缺点: 计算复杂度高.空间复杂度高. (2)KNN模型的三个要素 kNN算法模型实际上就 ...

  9. HP LaserJet M602 更換碳粉盒CE390XC

    HP LaserJet M602 原裝碳粉盒為 CE390A 且容量小不夠用,故更換大號的 CE390XC ,需要將 CE390XC 外面的所有橙色部件去掉(取走上面一條帶2個凸起的遮罩,左側有一個耳 ...

  10. liunx基本操作命令

    1.  rm  删除命令 rm   -i  文件名-----------询问y/N 删除文件 rm  -f  文件名-----------直接删除文件 rm  -r  目录 rm   文件名 rm  ...