springBoot学习笔记三:基础配置之配置文件application.yaml

一、项目说明

项目环境:jdk1.8+tomcat8+idea2018

源代码github地址:

实现目标:springBoot中对于项目的依赖,插件等都采用了自动化配置,但是在实际生产中难免会手动配置一些内容。在使用spring构建项目时一般使用.xml/.properties文件保存配置,在springBoot构建的项目中也可以使用.properties作为配置文件或者使用.yaml。这里通过.yaml的方式作为配置文件,了解其基本的配置与使用。

二、配置说明

(1).yaml文件概述

        yaml也是一种专门用来编写配置文件的语言,与properties作用类似,yaml利用缩进表示层级关系且区分大小写。

(2)配置文件存放的位置

        注:配置文件可以放在如下四个位置,四个位置的加载顺序与标注的序号一致。

a5b51199152573a9d516c1d509ca3a1b26a.jpg

(3)代码说明

        A:application.yaml

user:   #d配置对象
  username: 张三
  usersex: 男
  userage: 18
  useraddress: 中国
  interest:   #配置list
    - 读书
    - 写字
    - 画画
  color: red,white,orange   #配置数组
  dress:    #配置map
    shoes: 耐克
    jack: 七匹狼
  colleague:    #配置list<map>
    - name: 同事一
      sex: 男
    - name: 同事二
      sex: 女

        B:User映射实体类

                @Component:实体类注解

                 @ConfigurationProperties(prefix = "user"):配置从yaml文件中读取以user为前缀的数据   

package com.example.model;

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

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "user")
public class User {
    private String userName;
    private Integer userAge;
    private String userAddress;
    private String userSex;
    private List<String> interest;
    private String[] color;
    private Map<String,String> dress;
    private List<Map<String,String>> colleague;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getUserAge() {
        return userAge;
    }

    public void setUserAge(Integer userAge) {
        this.userAge = userAge;
    }

    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex = userSex;
    }

    public List<String> getInterest() {
        return interest;
    }

    public void setInterest(List<String> interest) {
        this.interest = interest;
    }

    public String[] getColor() {
        return color;
    }

    public void setColor(String[] color) {
        this.color = color;
    }

    public Map<String, String> getDress() {
        return dress;
    }

    public void setDress(Map<String, String> dress) {
        this.dress = dress;
    }

    public List<Map<String, String>> getColleague() {
        return colleague;
    }

    public void setColleague(List<Map<String, String>> colleague) {
        this.colleague = colleague;
    }

    public User(String userName, Integer userAge, String userAddress, String userSex, List<String> interest, String[] color, Map<String, String> dress, List<Map<String, String>> colleague) {
        this.userName = userName;
        this.userAge = userAge;
        this.userAddress = userAddress;
        this.userSex = userSex;
        this.interest = interest;
        this.color = color;
        this.dress = dress;
        this.colleague = colleague;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", userAge=" + userAge +
                ", userAddress='" + userAddress + '\'' +
                ", userSex='" + userSex + '\'' +
                ", interest=" + interest +
                ", color=" + Arrays.toString(color) +
                ", dress=" + dress +
                ", colleague=" + colleague +
                '}';
    }
}

        C:HelloSpringbootController

package com.example.controller;

import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloSpringbootController {

    @Autowired User user;

    @GetMapping("/springBoot")
    public String sayHello(){
        return "Hello Springboot!";
    }

    @GetMapping("/getUser")
    public String getUser(){
        return user.toString();
    }

}

(4)启动项目,并访问

7ccb590a57343722da2f9ff060f714548e8.jpg

 

 

 

 

 

转载于:https://my.oschina.net/tij/blog/3015244

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值