您好,登錄后才能下訂單哦!
這篇文章主要介紹了Springboot中@Value怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
entity.Book
package com.draymonder.amor.entity;import java.util.List;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Componentpublic class Book { @Value("${book.name}") private String name; @Value("${book.author}") private String author; @Value("${book.price}") private Double price; @Value("#{'${book.love}'.split(',')}") private List<String> love; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", love=" + love + '}'; }}
web.BookController
package com.draymonder.amor.web;import com.draymonder.amor.entity.Book;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class BookController { @Autowired Book book; @GetMapping("/book") public String book() { return book.toString(); }}
resources/applcation.yml
server: port: 8080book: name: amor author: draymonder price: 50 love: a, b, c
訪問url localhost:8080/book
展示結果
Book{name='amor', author='draymonder', price=50.0, love=[a, b, c]}
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Springboot中@Value怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。