亲宝软件园·资讯

展开

SpringBoot yaml配置文件

鸣鼓ming 人气:0

1.基本语法

2.数据类型

1.字面量:单个的、不可再分的值。date、boolean、string、number、null

k: v

2.对象:键值对的集合。map、hash、set、object

#行内写法:  

k: {k1:v1,k2:v2,k3:v3}

#或

k: 
  k1: v1
  k2: v2
  k3: v3

3.数组:一组按次序排列的值。array、list、queue

#行内写法:  

k: [v1,v2,v3]

#或者

k:
 - v1
 - v2
 - v3

3.代码测试

User

package com.limi.springboottest2.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String userName;
    private Integer age;
    private String gender;
}

Entity1

package com.limi.springboottest2.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@ConfigurationProperties(prefix = "entity1")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Entity1 {
    private Double number;
    private List<String> array;
    private User user;
    private Map<String, Integer> map;
    private String str0;
    private String str1;
    private String str2;
}

application.yml

entity1:
  number: 11
  array: ["apple", "peach", "orange"]
  user: {userName: "lily", }
  map: {"Math": 100,"English": 98,"Art": 8}
#对比字符串变量不使用引号、使用单引号、双引号的区别
  str0: \n 666
  str1: '\n 666'
  str2: "\n 666"

HelloController

package com.limi.springboottest2.controller;
import com.limi.springboottest2.entity.Entity1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
    @Autowired
    private Entity1 entity1;
    @GetMapping("/test1")
    @ResponseBody
    void test1() {
        System.out.println(entity1);
    }
}

测试结果

可以看到

4.开启补全提示

就是下图的效果

自定义的类和配置文件绑定一般没有提示。若要提示,添加如下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
<!-- 下面插件作用是工程打包时,不将spring-boot-configuration-processor打进包内,让其只在编码的时候有用 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

加载全部内容

相关教程
猜你喜欢
用户评论