當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot中通过@Value获取自定义配置的值
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot中通过@Value获取自定义配置的值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
在SpringBoot項目中的application.properties中定義變量,要在
controller中獲取自定義配置的值。
實現
打開
application.properties
添加如下代碼
book.author=Badao book.name=SpringBoot新建BookController.java
package com.example.demo.controller;import javax.sound.midi.MidiDevice.Info;import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;@EnableAutoConfiguration @Controller public class BookController {@Value("${book.author}")private String author;@Value("${book.name}")private String name;@RequestMapping("/bookInfo")@ResponseBodypublic String showInfo() {return author+":"+name;}public static void main(String[] args) {SpringApplication.run(BookController.class, args);}}右鍵run as --java application
?
效果
打開瀏覽器輸入:
http://localhost:8080/bookInfo
總結
以上是生活随笔為你收集整理的SpringBoot中通过@Value获取自定义配置的值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS实现限制input上传文件的大小和格
- 下一篇: Jquery中使用ajax传json参数