赞
踩
常规配置
YAML是JSON的超集,简洁而强大,是一种专门用来书写配置文件的语言,可以替代application.properties。
在创建一个Spring Boot项目时,引入的spring-boot-starter-web依赖间接地引入了snakeyaml依赖,snakeyaml会实现对YAML配置的解析。YAML的使用非常简单,利用缩进来表示层级关系,并且大小写敏感。
在Spring Boot项目中使用YAML只需要在resources目录下创建一个application.yml文件即可,然后向application.yml中添加如下配置:
- server:
- port: 8099
- servlet:
- context-path: /
- tomcat:
- uri-encoding: UTF-8
这一段配置等效于application.properties中的如下配置:
- server.port=8099
- server.servlet.context-path=/
- server.tomcat.uri-encoding=UTF-8
此时可以将resources目录下的application.properties文件删除,完全使用YAML完成文件的配置。
复杂配置
YAML不仅可以配置常规属性,也可以配置复杂属性,例如下面一组配置:
- my:
- name: 江南皮革
- address: 中国
像Properties配置文件一样,这一段配置也可以注入一个Bean中,代码如下:
- @Component
- @ConfigurationProperties(prefix = "my")
- public class User
- {
- private String name;
- private String address;
- private String[] favorites;
YAML还支持列表的配置,例如下面一组配置:
- my:
- name: 江南皮革
- address: 中国
- favorites:
- - 数组1
- - 数组2
- - 数组3
- hobby:
- - 元素1
- - 元素2
- - 元素3
这一组配置可以注入如下Bean中:
- @Component
- @ConfigurationProperties(prefix = "my")
- public class User
- {
- private String name;
- private String address;
- private String[] favorites;
- private List<String> hobby;
YAML还支持更复杂的配置,即集合中也可以是一个对象,例如下面一组配置:
- you:
- productDetailList:
- - name: 江南皮革
- address: 中国
- favorites:
- - 数组1
- - 数组2
- - 数组3
- - name: 西北牛皮
- address: 西安
- favorites:
- - 数组1
- - 数组2
- - 数组3
这组配置在集合中放的是一个对象,因此可以注入如下集合中:
- @Component
- @ConfigurationProperties("you")
- public class Product
- {
- private List<ProductDetail> productDetailList;
- @Component
- public class ProductDetail
- {
- private String name;
- private String address;
- private List<String> favorites;
在Spring Boot中使用YAML虽然方便,但是YAML也有一些缺陷,例如无法使用@PropertySource注解加载YAML文件,如果项目中有这种需求,还是需要使用Properties格式的配置文件。
下面是项目具体情况截图
代码如下:
application.yml
- server:
- port: 8099
- servlet:
- context-path: /
- tomcat:
- uri-encoding: UTF-8
-
- my:
- name: 江南皮革
- address: 中国
- favorites:
- - 数组1
- - 数组2
- - 数组3
- hobby:
- - 元素1
- - 元素2
- - 元素3
-
- you:
- productDetailList:
- - name: 江南皮革
- address: 中国
- favorites:
- - 数组1
- - 数组2
- - 数组3
- - name: 西北牛皮
- address: 西安
- favorites:
- - 数组1
- - 数组2
- - 数组3
-
User.java
- package com.shrimpking.pojo;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- import java.util.Arrays;
- import java.util.List;
-
- /**
- * @author user1
- */
- @Component
- @ConfigurationProperties(prefix = "my")
- public class User
- {
- private String name;
- private String address;
- private String[] favorites;
- private List<String> hobby;
-
- public User()
- {
- }
-
- public User(String name, String address, String[] favorites, List<String> hobby)
- {
- this.name = name;
- this.address = address;
- this.favorites = favorites;
- this.hobby = hobby;
- }
-
- public String getName()
- {
- return name;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-
- public String getAddress()
- {
- return address;
- }
-
- public void setAddress(String address)
- {
- this.address = address;
- }
-
- public String[] getFavorites()
- {
- return favorites;
- }
-
- public void setFavorites(String[] favorites)
- {
- this.favorites = favorites;
- }
-
- public List<String> getHobby()
- {
- return hobby;
- }
-
- public void setHobby(List<String> hobby)
- {
- this.hobby = hobby;
- }
-
- @Override
- public String toString()
- {
- return "User{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", favorites=" + Arrays.toString(favorites) + ", hobby=" + hobby + '}';
- }
- }
UserController.java
- package com.shrimpking.controller;
-
- import com.shrimpking.pojo.User;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author user1
- */
- @RestController
- public class UserController
- {
- @Autowired
- private User user;
-
- @GetMapping("/user")
- public String user()
- {
- return user.toString();
- }
- }
Product.java
- package com.shrimpking.pojo;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- import java.util.List;
-
- /**
- * @author user1
- */
- @Component
- @ConfigurationProperties("you")
- public class Product
- {
- private List<ProductDetail> productDetailList;
-
- public Product()
- {
- }
-
- public List<ProductDetail> getProductDetailList()
- {
- return productDetailList;
- }
-
- public void setProductDetailList(List<ProductDetail> productDetailList)
- {
- this.productDetailList = productDetailList;
- }
-
- @Override
- public String toString()
- {
- return "Product{" + "productDetailList=" + productDetailList + '}';
- }
- }
ProductDetail.java
- package com.shrimpking.pojo;
-
- import org.springframework.stereotype.Component;
-
- import java.util.List;
-
- /**
- * @author user1
- */
- @Component
- public class ProductDetail
- {
- private String name;
- private String address;
- private List<String> favorites;
-
- public ProductDetail()
- {
- }
-
- public ProductDetail(String name, String address, List<String> favorites)
- {
- this.name = name;
- this.address = address;
- this.favorites = favorites;
- }
-
- public String getName()
- {
- return name;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-
- public String getAddress()
- {
- return address;
- }
-
- public void setAddress(String address)
- {
- this.address = address;
- }
-
- public List<String> getFavorites()
- {
- return favorites;
- }
-
- public void setFavorites(List<String> favorites)
- {
- this.favorites = favorites;
- }
-
- @Override
- public String toString()
- {
- return "ProductDetail{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", favorites=" + favorites + '}';
- }
- }
ProductController.java
- package com.shrimpking.controller;
-
- import com.shrimpking.pojo.Product;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author user1
- */
- @RestController
- public class ProductController
- {
- @Autowired
- private Product product;
-
- @GetMapping("/product")
- public String test()
- {
- return product.toString();
- }
- }
运行截图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。