SpringBoot读取application.properties文件

springboot 配置文件 .properties和.yml的写法区别

例如 :    redis配置的properties或yml文件,如下:

  1. spring.redis.cluster.nodes[0]=192.168.0.1:6379  
  2. spring.redis.cluster.nodes[1]=192.168.0.2:6379  
  3. 或  
  4. spring:  
  5.    redis:  
  6.       cluster:  
  7.          nodes:  
  8.             - 192.168.0.1:6379  
  9.             - 192.168.0.2:6379  

spring boot允许你自定义一个application.properties文件,然后放在以下的地方,来重写spring boot的环境变量或者定义你自己环境变量

  1. 当前目录的 “/config”的子目录下
  2. 当前目录下
  3. classpath根目录的“/config”包下
  4. classpath的根目录下

1点和2点适合在生产环境下,例如,打包成可执行的jar包

这里写图片描述

这里要注意,“当前目录”是指demo.jar包的目录下,要使配置文件生效,在使用Java -jar demo.jar的命令时,必须先路由到demo.jar包的路径下,再使用其命名,

这里写图片描述

3点和4点适合在开发环境下

这里写图片描述

如果同时在四个地方都有配置文件,配置文件的优先级是从1到4。

使用配置文件之后,spring boo启动时,会自动把配置信息读取到spring容器中,并覆盖spring boot的默认配置,那么,我们怎么来读取和设置这些配置信息呢

1.通过命令行来重写和配置环境变量,优先级最高,例如可以通过下面的命令来重写spring boot 内嵌tomcat的服务端口,注意“=”俩边不要有空格

java -jar demo.jar --server.port=9000
  • 1
  • 1

如果想要设置多个变量怎么办,可以已json的格式字符串来设置

java -jar demo.jar --spring.application.json='{"foo":"bar"}'
  • 1
  • 1

2.通过@value注解来读取

@RestController
@RequestMapping("/task")
public class TaskController { @Value("${connection.remoteAddress}") private String address; @RequestMapping(value = {"/",""}) public String hellTask(@Value("${connection.username}")String name){ return "hello task !!"; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.通过Environment接口来获取,只需要把接口注进去即可

@RestController
@RequestMapping("/task")
public class TaskController { @Autowired Environment ev ; @Value("${connection.remoteAddress}") private String address; @RequestMapping(value = {"/",""}) public String hellTask(@Value("${connection.username}")String name){ String password = ev.getProperty("connection.password"); return "hello task !!"; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

4.可以自定义一个工具类,来获取,这种方式关键在于读取配置文件信息,适合自定义的配置信息,spring 容器默认的配置信息会读不到

@Component
public class SystemConfig { private static Properties props ; public SystemConfig(){ try { Resource resource = new ClassPathResource("/application.properties");// props = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { e.printStackTrace(); } } /** * 获取属性 * @param key * @return */ public static String getProperty(String key){ return props == null ? null : props.getProperty(key); } /** * 获取属性 * @param key 属性key * @param defaultValue 属性value * @return */ public static String getProperty(String key,String defaultValue){ return props == null ? null : props.getProperty(key, defaultValue); } /** * 获取properyies属性 * @return */ public static Properties getProperties(){ return props; } } //用的话,就直接这样子 String value = SystemConfig.getProperty("key");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

5.可以利用${…}在application.properties引用变量

myapp.name=spring
myapp.desc=${myapp.name} nice
  • 1
  • 2
  • 1
  • 2

6.可以在application.properties配置随机变量,利用的是RandomValuePropertySource类

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long} my.number.less.than.ten=${random.int(10)} my.number.in.range=${random.int[1024,65536]}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

简单的配置文件的使用就先写到这里,再看看其他高级用法,如Profiles还有@ConfigurationProperties

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

SpringBoot读取application.properties文件,通常有3种方式

1. @Value  例如: 

@Value("${spring.profiles.active}")

private String profileActive;------相当于把properties文件中的spring.profiles.active注入到变量profileActive中

2. @ConfigurationProperties  例如:

 

@Component
@ConfigurationProperties(locations = "classpath:application.properties",prefix="test")
public class TestProperties {
String url;
String key;

}

其他类中使用时,就可以直接注入该TestProperties 进行访问相关的值

3. 使用Enviroment   例如:

private Enviroment env;

env.getProperty("test.url");

而env方式效率较低

 

注:@ConfigurationProperties也可用于其他.properties文件,只要locations指定即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值