02. yaml数据读取方式

在spring boot项目中,的resources中创建application.yaml文件

lesson: SpringBoot
server:
  port: 8080

enterprise:
  name: itcast
  age: 16
  tel: 10011
  subject:
    - Java
    - Forward
    - BigData

BookController文件中,读取application.yaml

第一种读取方式:用变量来接收application.yaml的值 

BookController类

package com.example.springboot_01_quickstart.Controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther CharlieLiang
 * @date 2022/6/2-20:23
 */
@RestController
@RequestMapping("/books")
public class BookController {

    @Value("${lesson}")
    private String lesson;
    @Value("${server.port}")
    private Integer port;
    @Value("${enterprise.subject[0]}")
    private String subject_0;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("id===>"+id);
        System.out.println(lesson);
        System.out.println(port);
        System.out.println(subject_0);
        return "Hello , springboot";
    }
}

项目启动后,在浏览器输入http://localhost:8080/books/1http://localhost:8080/books/1运行结果,IdeaRun窗口打印信息

 第二种读取方法:借助Environment变量

 BookController类

package com.example.springboot_01_quickstart.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther CharlieLiang
 * @date 2022/6/2-20:23
 */
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
   private Environment environment;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println(environment.getProperty("lesson"));
        System.out.println(environment.getProperty("server.port"));
        System.out.println(environment.getProperty("enterprise.age"));
        System.out.println(environment.getProperty("enterprise.subject[1]"));
        return "Hello , springboot";
    }
}

运行结果:

 第三种方法:用bean接受application.yaml的信息

 Enterprise类

package com.example.springboot_01_quickstart.domain;

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

import java.util.Arrays;

/**
 * @auther CharlieLiang
 * @date 2022/6/2-23:49
 */
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private Integer age;
    private String tel;
    private String[] subject;

    @Override
    public String toString() {
        return "Enterprise{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", tel='" + tel + '\'' +
                ", subject=" + Arrays.toString(subject) +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String[] getSubject() {
        return subject;
    }

    public void setSubject(String[] subject) {
        this.subject = subject;
    }


}

 BookController类

package com.example.springboot_01_quickstart.Controller;

import com.example.springboot_01_quickstart.domain.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther CharlieLiang
 * @date 2022/6/2-20:23
 */
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
   private Environment environment;

    @Autowired
    private Enterprise enterprise;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println(environment.getProperty("lesson"));
        System.out.println(environment.getProperty("server.port"));
        System.out.println(environment.getProperty("enterprise.age"));
        System.out.println(environment.getProperty("enterprise.subject[1]"));
        System.out.println("==========================");
        System.out.println(enterprise);
        return "Hello , springboot";
    }
}

运行结果:

 说明:自定义对象封装数据警告解决方案

<!--        去掉Enterprise的警告-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值