久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

springboot如何使用@Value獲取配置文件的值

瀏覽:80日期:2023-06-26 09:00:02
使用@Value獲取配置文件的值1、創(chuàng)建配置文件(application.properties)

spring.activemq.broker-url=tcp://localhost:61616spring.activemq.user=adminspring.activemq.password=adminspring.activemq.in-memory=truespring.activemq.pool.enabled=false2、創(chuàng)建測試類(MyController.java)

package com.jeff.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class MyController {@Value('${spring.activemq.user}')private String user;@RequestMapping('myTest')public String myTest() {System.out.println('user的值:' + user);return 'success';}}3、打開瀏覽器訪問 http://localhost:8080/myTest,控制臺(tái)輸出結(jié)果

springboot如何使用@Value獲取配置文件的值

springboot如何使用@Value獲取配置文件的值

SpringBoot 使用@Value()注解獲取到配置文件中的值為null一、描述

@Slf4j@Componentpublic class FileUtils { @Value('${document.path.output}') private String outPath; }document: path: output: E:/workspace/doc_convert/tmp二、解決方式

新創(chuàng)建了一個(gè)SpringBoot項(xiàng)目,在使用@Value獲取配置文件中的值時(shí),一直為null。

通過排查發(fā)現(xiàn),雖然值為null,說明配置的參地址是正確的,只是在Spring進(jìn)行依賴注入的時(shí)候沒有把這個(gè)值注入到變量中來。

通過檢查發(fā)現(xiàn),是由于使用此類(注解所在的類)時(shí)候是直接new的,并沒有將此類的創(chuàng)建交由Spring容器進(jìn)行管理。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 乾安县| 图片| 普定县| 青铜峡市| 清丰县| 潜江市| 丹江口市| 万盛区| 彭泽县| 尼勒克县| 隆尧县| 黄陵县| 泾源县| 阳西县| 明溪县| 哈尔滨市| 时尚| 重庆市| 扎赉特旗| 达拉特旗| 富平县| 尚义县| 松溪县| 电白县| 松江区| 宁波市| 成都市| 吴忠市| 静宁县| 洛浦县| 渭南市| 甘孜县| 水城县| 黄冈市| 宁德市| 微山县| 高阳县| 永康市| 油尖旺区| 鲁甸县| 瑞昌市|