前言:Spring-boot的yml配置文件,这里就不在借助人、狗介绍了,试试套下微信!

创建yml文件

  • 值得注意的是下图中有三种命名方法,前两种是对的,且第二种必须是横线而不是下划线!

  1. yml文件的基本语法:由键值对和空格组成

    • 值得注意的是:作为yml的固定格式,如wxmessage、contacts这样的键后面,都需要加一个空格

    weixin:
    wxmessage: message
    contacts: friends
    discover: find_list
    me: owner_message
  2. yml的Map写法:一般情况下好友列表都会有ID,对应的昵称,头像地址,我们这里拿不到id,

    则可以将好友列表当做Map

    • 这里需要注意的是再使用IDEA 编辑配置文件的时候,如果发现配置文件的编辑项显示异常,

      比如:contacts :{image_qiankui: "百人钱坤",
      image_qianmo: "百人阡陌"*},

      那么很有可能是配置文件的类型和实际映射的实体类型不一致,即:在java文件里定义了

      一个属性:private Contacts contacts,实体却定义成Map类型,就会出错或者值为null

    weixin:
    contacts :{image_qiankui: "百人钱坤",image_qianmo: "百人阡陌",}
  3. yml的 数组写法:微信的发现表面看很明显是一个List

      #  第一种数组写法
    find-list: [moments,Scan QR Code,Shake]
    # 第二种数组写法
    find-list:
    - moments
    - Scan QR Code
    - Shake
  4. yml的对象写法:“我”模块中包含着用户的昵称、头像、支付、相册等,可以当做一个对象来处理。

    me:
    wxNickName: .
    wxId: miao90***
    wxPay: payAdress
  5. 配置文件相关的两个注解

    //将对象注入到容器内

    @Component

    //标注在类名上表示该类对应配置文件的“weixin”

    @ConfigurationProperties(prefix = "weixin")

总结: 以上四种yml的常用写法,除第一种很少能用上之外,其他的三种看似简单,但经常会因为空格层级关系不明确、实体类型映射不匹配等问题导致项目运行不成功!

附:测试相关代码

package com.wujianqinjian.springboot_note.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import java.util.List;
import java.util.Map; @Component
@ConfigurationProperties(prefix = "weixin")
public class Weixin {
private List<String> message;
private Map<String,String> contacts;
private List<String> findList;
private Me me; @Override
public String toString() {
return "Weixin{" +
"message=" + message +
", contacts=" + contacts +
", findList=" + findList +
", me=" + me +
'}';
} public List<String> getMessage() {
return message;
}
public void setMessage(List<String> message) {
this.message = message;
} public Map<String, String> getContacts() {
return contacts;
} public void setContacts(Map<String, String> contacts) {
this.contacts = contacts;
} public List<String> getFindList() {
return findList;
} public void setFindList(List<String> findList) {
this.findList = findList;
} public Me getMe() {
return me;
} public void setMe(Me me) {
this.me = me;
}
}
package com.wujianqinjian.springboot_note.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "contacts")
public class Contacts {
private String imageUrl;
private String nickName; @Override
public String toString() {
return "Contacts{" +
"imageUrl='" + imageUrl + '\'' +
", nickName='" + nickName + '\'' +
'}';
} public String getNickName() {
return nickName;
} public void setNickName(String nickName) {
this.nickName = nickName;
} public String getImageUrl() {
return imageUrl;
} public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
package com.wujianqinjian.springboot_note.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "me")
public class Me {
private String wxNickName;
private String wxId;
private String wxPay;
private String wxCollect;
private String wxPhoto;
private String wxCards;
private String wxExpression; @Override
public String toString() {
return "Me{" +
"wxNickName='" + wxNickName + '\'' +
", wxId='" + wxId + '\'' +
", wxPay='" + wxPay + '\'' +
", wxCollect='" + wxCollect + '\'' +
", wxPhoto='" + wxPhoto + '\'' +
", wxCards='" + wxCards + '\'' +
", wxExpression='" + wxExpression + '\'' +
'}';
} public String getWxNickName() {
return wxNickName;
} public void setWxNickName(String wxNickName) {
this.wxNickName = wxNickName;
} public String getWxId() {
return wxId;
} public void setWxId(String wxId) {
this.wxId = wxId;
} public String getWxPay() {
return wxPay;
} public void setWxPay(String wxPay) {
this.wxPay = wxPay;
} public String getWxCollect() {
return wxCollect;
} public void setWxCollect(String wxCollect) {
this.wxCollect = wxCollect;
} public String getWxPhoto() {
return wxPhoto;
} public void setWxPhoto(String wxPhoto) {
this.wxPhoto = wxPhoto;
} public String getWxCards() {
return wxCards;
} public void setWxCards(String wxCards) {
this.wxCards = wxCards;
} public String getWxExpression() {
return wxExpression;
} public void setWxExpression(String wxExpression) {
this.wxExpression = wxExpression;
}
}

Springboot2(二)通过微信熟悉熟悉Spring-boot yml配置文件的更多相关文章

  1. Spring Boot 的配置文件application.properties

    Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...

  2. Spring Boot之配置文件值注入(@ConfigurationProperties)

    前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...

  3. Spring Boot属性配置文件:application.properties 详解

    学习资料 网址 官方说明文档 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-pro ...

  4. 一:Spring Boot 的配置文件 application.properties

    Spring Boot 的配置文件 application.properties 1.位置问题 2.普通的属性注入 3.类型安全的属性注入 1.位置问题 当我们创建一个 Spring Boot 工程时 ...

  5. Spring Boot 核心配置文件 bootstrap & application

    Spring Boot 核心配置文件 bootstrap & application 1.SpringBoot bootstrap配置文件不生效问题 2.bootstrap/ applicat ...

  6. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件

    精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件 内容简介:本文介绍 Spring Boot 的配置文件和配置管理,以及介绍了三种读取配置文 ...

  7. spring boot全局配置文件优先级

    前两篇介绍的application配置文件,即为spring boot全局配置文件.那么spring boot加载配置文件的时候,怎么确定加载哪个目录下哪个文件呢? spring boot默认的配置文 ...

  8. spring boot:用zxing生成二维码,支持logo(spring boot 2.3.2)

    一,zxing是什么? 1,zxing的用途 如果我们做二维码的生成和扫描,通常会用到zxing这个库, ZXing是一个开源的,用Java实现的多种格式的1D/2D条码图像处理库. zxing还可以 ...

  9. Sping Boot入门到实战之入门篇(二):第一个Spring Boot应用

    该篇为Spring Boot入门到实战系列入门篇的第二篇.介绍创建Spring Boot应用的几种方法. Spring Boot应用可以通过如下三种方法创建: 通过 https://start.spr ...

随机推荐

  1. SpringBoot2整合Shiro报错 UnavailableSecurityManagerException: No SecurityManager accessible to the calling code 【已解决】

    SpringBoot集成Shiro报错 UnavailableSecurityManagerException: No SecurityManager accessible to the callin ...

  2. 7.vue前台配置、插件、组件

    目录 luffy前台配置 axios前后台交互 cookies操作 element-ui页面组件框架 bootstrap页面组件框架 luffy前台配置 axios前后台交互 安装:前端项目目录下的终 ...

  3. 给 ABP vNext 应用安装私信模块

    在上一节五分钟完成 ABP vNext 通讯录 App 开发 中,我们用完成了通讯录 App 的基础开发. 这本章节,我们会给通讯录 App 安装私信模块,使不同用户能够通过相互发送消息,并接收新私信 ...

  4. 普通人学习rust——从零到放弃 变量、不可变量、常量

    普通人学习rust--从零到放弃 变量.不可变量.常量 环境 本文章内容基于如下环境,如若出入请参考当前环境. rustc 1.42.0 (b8cedc004 2020-03-09) cargo 1. ...

  5. [SQL]3.26--175+176+177+178+180+181+182

    175.组合两个表 题目 Code SELECT FirstName, LastName, City, State FROM Person LEFT JOIN Address --由于需要Person ...

  6. demo26-路径

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 洛谷1074 靶状数独dfs 排序、记录、搜索

    题目网址:https://www.luogu.com.cn/problem/P1074 大意就是在一个9*9的数独中填数,要求行列宫都是九个互不相同的数字,给定一定的得分机制,要求求解最大得分.思路大 ...

  8. 凸包问题 Graham Scan

    2020-01-09 15:14:21 凸包问题是计算几何的核心问题,并且凸包问题的研究已经持续了好多年,这中间涌现出了一大批优秀的算法. 凸包问题的最优解法是Graham Scan算法,该算法可以保 ...

  9. Building Applications with Force.com and VisualForce(Dev401)(十二):Implementing Business Processes:Automating Business Processes Part 1

    ev401-013:Implementing Business Processes:Automating Business Processes Part 1 Module Objectives1.Li ...

  10. 史上最详细的Docker安装手册

    概述: 这个安装手册我已经使用了将近2年的时间,一直在进行完善(可以用于生产级别).使用了Centos 7系统. 一.Docker简单介绍 Docker是一个容器,使用的是Linux现有的技术,准确来 ...