Spring Boot读取application.yaml属性

2020-10-27 16:09发布

Spring Boot读取application.yaml属性

一、访问实体属性

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

<dependency>
   <groupId>org.yaml</groupId>
   <artifactId>snakeyaml</artifactId>
   <version>1.18</version>
</dependency>

2、创建applicaiton.yaml文件

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

二、访问列表属性

1、添加warehouse属性

2、创建Product实体类

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类

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<Product> products;
 
    public List<Product> getProducts() {
        return products;
    }
 
    public void setProducts(List<Product> 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



作者:howard2005

链接:https://blog.csdn.net/howard2005/article/details/79346555

来源:CSDN
著作权归作者所有,转载请联系作者获得授权,切勿私自转载。