springboot的配置信息的设置和读取(application.properties/application.yml)

springboot提供了两种配置信息的文件格式,application.properties和application.yml,基于直接明了,使用方便和高效的前提下下面的配置均采用yml格式配置,

注意

  1. yml采用缩减方式来排列
  2. 键后面紧跟冒号,然后空格,最后是值
  3. 注意里面用到爱好数字的配置信息每个数组值前面的-(横杠)和值之间有一个空格

properties也yml/ymal相互转换

在线yaml转properties-在线properties转yaml-ToYaml.com在线yaml转properties工具 - toyamlhttps://www.toyaml.com/

配置信息的分类(三种)

具体可以查看官网

Spring Boot Reference Documentationhttps://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#legal

springboot标准的配置

例如web服务的端口和上下文路径

Groovy 复制代码
server:
  port: 9999
  servlet:
    context-path: /yuanma

其他第三方配置信息

例如集成redis

Groovy 复制代码
spring:
  data:
    redis:
      host: "localhost"
      port: 6379
      database: 0
      username: "user"
      password: "secret"

自定义配置信息(两种获取信息的方式)

@Value+EL表达式

例如下面配置了邮件信息

Groovy 复制代码
#邮件信息
mail:
  from: 123@qq.com
  to: 456@qq.com
  server: smtp.qq.com
  port: 8888

下面用@Value+EL表达式配置

java 复制代码
package com.burns.yuanma.admin.pojo;

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

@Component
@Data
public class MailProperties {

    /**
     * 发件人
     */
    @Value("${mail.from}")
    private String from;

    /**
     * 收件人
     */
    @Value("${mail.to}")
    private String to;

    /**
     * 服务器地址
     */
    @Value("${mail.server}")
    private String server;

    /**
     * 服务器端口号
     */
    @Value("${mail.port}")
    private String port;
}

@ConfigurationProperties+prefix(前缀)

例如下面的用户信息配置,

Groovy 复制代码
#用户信息
user:
  address: 北京市朝阳区安外大羊坊八号院8号楼2单元112
  name: 张三
  age: 12
  sex: 男
  hobbies:
    - 足球
    - 游泳
    - 骑行
    - 篮球
    - 棒球

获取用户信息

java 复制代码
package com.burns.yuanma.admin.pojo;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "user")
@Data
public class UserProperties {

    /**
     * 姓名
     */
    private String name;

    /**
     * 年齡
     */
    private int age;

    /**
     * 性別
     */
    private String sex;

    /**
     * 郵件
     */
    private String email;

    /**
     * 住址
     */
    private String address;

    /**
     * 愛好
     */
    private String[] hobbies;
}

访问接口获取信息

上面两种方式都可以获取信息

下面是controller

java 复制代码
package com.burns.yuanma.admin.controller;

import com.burns.yuanma.admin.pojo.MailProperties;
import com.burns.yuanma.admin.pojo.UserProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PropertiesController {



    @Autowired
    private UserProperties userProperties;

    @Autowired
    private MailProperties mailProperties;
    @RequestMapping("/userInfo")
    public String userInfo(){
        System.out.println(userProperties.toString());
        return userProperties.toString();
    }

    @RequestMapping("/mailInfo")
    public String mailInfo(){
        System.out.println(mailProperties.toString());
        return mailProperties.toString();
    }

}

访问浏览器

用户信息

http://localhost:9999/yuanma/userInfohttp://localhost:9999/yuanma/userInfo

邮件信息

http://localhost:9999/yuanma/mailInfohttp://localhost:9999/yuanma/mailInfo

相关推荐
float_六七4 小时前
IntelliJ IDEA双击Ctrl的妙用
java·ide·intellij-idea
能摆一天是一天5 小时前
JAVA stream().flatMap()
java·windows
颜如玉6 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
程序员爱钓鱼6 小时前
Go语言实战案例 — 工具开发篇:实现一个图片批量压缩工具
后端·google·go
程序员的世界你不懂7 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年7 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
gb42152878 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
曾经的三心草8 小时前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器
Metaphor6928 小时前
Java 高效处理 Word 文档:查找并替换文本的全面指南
java·经验分享·word
ChinaRainbowSea8 小时前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程