springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容

springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容

直入主题,咱为了条理性 还是新构建一个新项目

所需依赖 lombok 非必须 主要是为了简化pojo 释放双手,用过的都说好!

    
      org.springframework.boot
      spring-boot-starter-web
    

    
      org.projectlombok
      lombok
      true
    

个人使用的读取配置文件的方式有三种

1.@ConfigurationProperties 注解

2.@Value注解

3.spring框架中的 Environment

(一)读取Springboot项目自身配置文件 yml或者 properties

首先 在配置文件中 自定义配置如下

构造了一个配置 配置中包含 String 和基本类型 以及一个list集合,接下来 咱们进行读取这个配置文件

#构造一个对象 
leilei:
  #名字
  name: leilei
  #年龄
  age: 22
  #性别
  sex: true
  #女朋友们
  girlfriends:
    - name: luci
      age: 21
      cup: C
    - name: meimei
      age: 22
      cup: D
    - name: keke
      age: 21
      cup: A
    - name: panghu
      age: 22
      cup: D

使用@value 获取尝试

获取 基本类型字段 获取 String ------成功

  @Value("${leilei.name}")
  private String name;
  @Value("${leilei.sex}")
  private String sex;

springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第1张图片springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第2张图片

获取集合 -----失败 启动报错

  @Value("${leilei.girlfriends}")
  private List girlfriends;

在这里插入图片描述
yml 中 - 形式的list子元素 ,@value 读取是有问题的,但是如果list或者map 是以 " ," 则可以使用@value 读取
例如:
springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第3张图片
我们可以在@value 中使用 #{}
进行包裹一层读取路径,然后就可以使用java api方法了
@Value之spring el解析Listspringboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第4张图片

 @Value("#{'${leilei.hobbys}'.split(',')}")

在这里插入图片描述
在这里插入图片描述

使用spring框架中的 Environment

首先,我们需要在使用该方法的类中(该类 也必须是交由Spring管理的) 注入进去

再调用其中的方法进行获取

获取 基本数据类型 以及String —成功

  @Autowired
  Environment environment;
  
  ##exmaple:
  @RequestMapping("test")
  public String test() {
    return environment.getProperty("leilei.name");
  }

springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第5张图片springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第6张图片

Environment 中的方法

springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第7张图片

使用 @ConfigurationProperties 注解 此注解是我读取配置最喜欢使用的一个注解

使用步骤

(1)按照配置文件 新建一个实体类

实体类中 字段名需与配置文件中的 相对应

  private String name;
  private Integer age;
  private Boolean sex;
  private List girlfriends = new ArrayList<>();

(2)打上 @ConfigurationProperties 注解 指明配置前缀

其意思就是 前缀加字段名 是必须可以找到的配置节点名

例如: 我这里前缀 是leilei 字段名有 name 那么他找配置文件中则是 leilei.name

@ConfigurationProperties(prefix = "leilei")

(3)交由Spring管理 实体提供get set toString 方法等

对应配置文件实体类则为:
springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第8张图片
使用方法 在需要使用的类下(该类也必须交由Spring管理)注入此实体bean

像 获取对象属性一般使用

eamaple:

  @Autowired
  private Author author;
  
   @RequestMapping("test")
  public Author test() {
    return author;
  }

springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第9张图片
那么到这里,我们再回过头来 ,看我们Springboot 项目中 yml 文件中的诸多配置 按住Ctrl 再点击配置节点 会跳转到响应的源码中 ,也会惊奇的发现 原来我们配置文件中编写的配置 spring框架也是通过这样来进行读取与装配的
比如我点击我们的 数据源配置
springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第10张图片springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第11张图片

他的前缀是 spring.datasource 字段名有 username password driverClassName 等等 ,是不是和我们自定义bean 前缀 leilei 字段有 name ,age等 读取配置的方式一模一样 !!!!

所以 个人建议 在使用复杂的配置 如数组啊 map啊 集合之类的 咱们直接使用
ConfigurationProperties 封装为bean 进行读取

类似 value 等方式可能还有一些因字段类型等 无法读取的局限性

那么读取springboot 项目自身配置文件 就完了

(二) 读取POM.xml配置文件信息

此操作结合了 在配置文件中编写配置 以及 使用 ConfigurationProperties 注解
读取pom 中的以下信息
springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第12张图片
我们这里在yml 配置文件中 需要使用特殊标签 @@
自定义配置节点名 @中间为POM.xml 节点 @ 例如 project.groupId
springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第13张图片
springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第14张图片读取pom文件bean数据

@ConfigurationProperties("myapplication")
@Data
@Component
public class ApplicationInfo {

  private String groupId;
  private String artifactId;
  private String version;
  private String name;
  private String description;
}

使用需要读取的类下注入实体bean

  @Autowired
  private ApplicationInfo applicationInfo;
  
  @RequestMapping("test")
  public ApplicationInfo test() {
    return applicationInfo;
  }

springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第15张图片

(三)读取额外配置文件

有的项目中 还有自定义配置的场景 例如 xxx.properties
自定义 leilei.properties 放于 resource目录下
springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第16张图片
配置文件中 填入内容
在这里插入图片描述

在读取自定义配置的时候 ,我们需要再了解一个注解@PropertySource 此注解 可以指定要读取的资源存放位置

我们使用此注解结合 ConfigurationProperties 来进行获取

自定义配置 读取装配bean 信息

我这里使用了 static 修饰字段 那么 在使用的时候 就不需要注入了 直接 类名.方法名即可获取配置信息

@PropertySource("classpath:leilei.properties")
@ConfigurationProperties(prefix = "jdbc")
@Component
public class MyProperties {

  private static String username;
  private static String password;

  public static String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    MyProperties.username = username;
  }

  public static String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    MyProperties.password = password;
  }
}

使用

  @RequestMapping("test")
  public String test() {
    return "读取自定义配置文件:" +
        "数据库账户:" + MyProperties.getUsername() + " 数据库密码:" + MyProperties.getPassword();
  }

springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第17张图片
那么 咱们的自定义配置文件 读取 也完成了
通过上述这么多案例 发现了 ConfigurationProperties 真的是个好东西 可以简化我们不少开发代码 以及降低项目维护难度

放上 全部测试 json 数据
springboot 读取配置文件 自定义配置文件 以及POM.xml 文件内容_第18张图片
附上项目源码:springboot-read-yml

你可能感兴趣的:(SpringBoot,spring,boot)