Spring Boot读取application.yaml属性

Spring Boot读取application.yaml属性

一、访问实体属性

Spring Boot读取application.yaml属性_第1张图片

1、pom.xml文件里添加snakeyaml依赖


   org.yaml
   snakeyaml
   1.18

2、创建applicaiton.yaml文件

Spring Boot读取application.yaml属性_第2张图片

3、创建BookSettings类

package net.hw.bean;

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

/**
 * Created by howard on 2017/4/1.
 */
@Component
@ConfigurationProperties(prefix = "book")
public class BookSettings {
    private int id;
    private String name;
    private double price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

4、创建BookController类

package net.hw.webmvc;

import net.hw.bean.BookSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by howard on 2017/4/1.
 */
@RestController
public class BookController {
    @Autowired
    private BookSettings book;

    @RequestMapping("/book")
    public String book() {
        return book.toString();
    }
}

5、启动程序,访问http://localhost:8080/book

Spring Boot读取application.yaml属性_第3张图片

二、访问列表属性

1、添加warehouse属性

Spring Boot读取application.yaml属性_第4张图片

2、创建Product实体类

Spring Boot读取application.yaml属性_第5张图片

package net.hw.bean;

/**
 * Created by howard on 2017/4/1.
 */
public class Product {
    private int id;
    private String name;
    private double price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

3、创建WarehouseSettings类

Spring Boot读取application.yaml属性_第6张图片

package net.hw.settings;

import net.hw.bean.Product;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by howard on 2017/4/1.
 */
@Component
@ConfigurationProperties(prefix = "warehouse")
public class WarehouseSettings {
    List products;

    public List getProducts() {
        return products;
    }

    public void setProducts(List products) {
        this.products = products;
    }
}

4、创建WarehouseController类

package net.hw.webmvc;

import net.hw.settings.WarehouseSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by howard on 2017/4/1.
 */
@RestController
public class WarehouseController {
    @Autowired
    private WarehouseSettings warehouse;

    @RequestMapping("/warehouse")
    public String warehouse() {

        return warehouse.getProducts().toString();
    }
}

5、启动程序,访问http://localhost:8080/warehouse


 

你可能感兴趣的:(Java,Web框架)