Value注解读取配置文件中的内容 Java随笔记

「这是我参与11月更文挑战的第10天,活动详情查看:2021最后一次更文挑战


相关文章

Java随笔记:Java随笔记


  • 为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性。
    1. 两种使用方法
      • 1)@Value(“#{configProperties[‘key’]}”)
      • 2)@Value(“${key}”)
    1. 配置文件示例
      • 1
        2
        3
        4
        5
        6
        makefile复制代码ftp:
        ftplp: 10.2.23.89
        ftpPort: 21
        ftpUser: uftp
        ftpPwd: 12345678
        ftpRemotePath: /home

说明:以上是配置文件中的信息,主要是一些账号密码等信息。

  1. 读取yml配置文件的工具类
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
52
53
54
55
56
57
58
59
60
61
62
63
typescript复制代码package com.dbright.dataprediction.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:ftpconfig.yml")
@ConfigurationProperties(prefix = "ftp")
public class FtpProperties {

  @Value("${ftplp}")
  public String ftplp;
  @Value("${ftpPort}")
  public String ftpPort;
  @Value("${ftpUser}")
  public String ftpUser;
  @Value("${ftpPwd}")
  public String ftpPwd;
  @Value("${ftpRemotePath}")
  public String ftpRemotePath;

  public String getFtplp() {
      return ftplp;
  }

  public void setFtplp(String ftplp) {
      this.ftplp = ftplp;
  }

  public String getFtpPort() {
      return ftpPort;
  }

  public void setFtpPort(String ftpPort) {
      this.ftpPort = ftpPort;
  }

  public String getFtpUser() {
      return ftpUser;
  }

  public void setFtpUser(String ftpUser) {
      this.ftpUser = ftpUser;
  }

  public String getFtpPwd() {
      return ftpPwd;
  }

  public void setFtpPwd(String ftpPwd) {
      this.ftpPwd = ftpPwd;
  }

  public String getFtpRemotePath() {
      return ftpRemotePath;
  }

  public void setFtpRemotePath(String ftpRemotePath) {
      this.ftpRemotePath = ftpRemotePath;
  }
}
  • 说明:以上是使用@value注解来读取yml配置文件的代码示例
+ 1)@component —— 把普通pojo实例化到spring容器中,相当于配置文件中的`<bean id="" class=""/>`
+ 2. @PropertySource("classpath:ftpconfig.yml") —— 设置yml文件的路径,方便扫描到。一般我们配置文件都是放在resources下。所以我们只需要 classpath+所需要读取的配置文件名称。
+ 3)@ConfigurationProperties(prefix = "ftp") —— 这个不需要解释太多,配置文件里面内容的前缀,我们读取的是ftp下的信息。
+ 4)@Value("${ftplp}") —— 这是读取我们所需的配置信息,美元符号+{字段名}即可制定
+ 5)下面定义字符串来接收所读取到的配置信息。
+ 6)写set和get方法,方便外部类调用。
  1. 演示:效果图如下 获取结果 在这里插入图片描述
  • 可以看到,我们成功取到了我们想要的值。
  1. 一开始说的第二种和这个差不多,把{}外的 $ 变成 # 号,然后里面指定配置文件的信息+字段而已。大同小异,我就不贴代码上来了。

以上内容可优化为动态读取不同环境下的配置文件的内容!优化内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
less复制代码@Component
@PropertySource("classpath:application-${spring.profiles.active}.yml")
@ConfigurationProperties(prefix = "entid")
public class AplicationUtils {

   @Value("${entName}")
   public String ftplp;

   public String getFtplp() {
       return ftplp;
  }

   public void setFtplp(String ftplp) {
       this.ftplp = ftplp;
  }
}

  • 在实际开发中,我们一般会有三个环境:
+ 开发环境
+ 测试环境
+ 线上环境.
+ 在不同的环境下读取的东西可能不一样,如:账号密码三个环境不一致,所以这时候我们需要用到动态读取配置文件中的内容.
![在这里插入图片描述](https://gitee.com/songjianzaina/juejin_p10/raw/master/img/15b379162888eb87c52d5540cc1d7f8f2c03c79c794d31b6506d1a6e60101889)
  • 配置文件加上@PropertySource(“classpath:application-${spring.profiles.active}.yml”),表明读取当前选中的环境读取其配置文件信息.
  • 今儿太忙,来不及写了。这是几年前的文章了,发上来凑个数,见笑了。

路漫漫其修远兮,吾必将上下求索~

如果你认为i博主写的不错!写作不易,请点赞、关注、评论给博主一个鼓励吧~hahah

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%