@Value如何獲取yml和properties配置參數(shù)
@Value獲取yml和properties配置參數(shù)

Yml:
#定時任務(wù)配置
application:
xxl:
job:
enabled: true
admin:
addresses: http:///yusp-job-admin/ #127.0.0.1:8080指網(wǎng)關(guān)ip:port,yusp-job-admin為調(diào)度中心服務(wù)名稱。通過網(wǎng)關(guān),注冊到微服務(wù)的/api/server接口,完成注冊動作
executor:
appname: af_job #執(zhí)行器名稱,要求務(wù)必唯一
ip: 10.21.126.237 #執(zhí)行器IP [選填]:默認(rèn)為空表示自動獲取IP,多網(wǎng)卡時可手動設(shè)置指定IP
port: 9097 #調(diào)度中心給微服務(wù)發(fā)送任務(wù),通過此端口發(fā)送指令
logpath: D:/temp #執(zhí)行器日志文件路徑
logretentiondays: 3 # 本地日志保存天數(shù),-1為永遠(yuǎn)保存
package com.xxljob.config;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import cn.com.yusys.yusp.commons.job.core.executor.XxlJobExecutor;
@Configuration
@ConditionalOnProperty(name = "application.xxl.job.enabled", havingValue = "true", matchIfMissing = false)
public class XxlJobAutoConfiguration {
private Logger logger = LoggerFactory.getLogger(XxlJobAutoConfiguration.class);
@Value("${application.xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${application.xxl.job.executor.appname}")
private String appName;
@Value("${application.xxl.job.executor.ip}")
private String ip;
@Value("${application.xxl.job.executor.port}")
private int port;
@Value("${application.xxl.job.executor.logpath}")
private String logPath;
@Value("${application.xxl.job.executor.logretentiondays}")
private int logRetentionDays;
public XxlJobAutoConfiguration() {
}
@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobExecutor xxlJobExecutor() throws IOException {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
xxlJobExecutor.setAdminAddresses(adminAddresses);
xxlJobExecutor.setAppName(appName);
xxlJobExecutor.setIp(ip);
xxlJobExecutor.setPort(port);
xxlJobExecutor.setLogPath(logPath);
xxlJobExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobExecutor;
}
}
Properties:

賦值:
@Value(“true”) 直接賦值
@value注解獲取yml文件中的值問題
在類中使用@Value注解獲取yml配置文件中的值時,需要注意:
1、yml文件中,當(dāng)值為0000
這種類型的值時,需要用雙引號將值引起來。
比如:
錯誤:key=0000
正確:key=“0000”
如果不使用雙引號的話,在使用@value注解時,得到的值是0,而不是0000
2、使用@Value注解得到的是null
需要使用@Autowired進(jìn)行注入,對應(yīng)類需要加上@Service
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中為bean指定InitMethod和DestroyMethod的執(zhí)行方法
在Spring中,那些組成應(yīng)用程序的主體及由Spring IoC容器所管理的對象,被稱之為bean,接下來通過本文給大家介紹Spring中為bean指定InitMethod和DestroyMethod的執(zhí)行方法,感興趣的朋友一起看看吧2021-11-11
SpringBoot項(xiàng)目修改訪問端口和訪問路徑的方法
這篇文章主要介紹了SpringBoot項(xiàng)目修改訪問端口和訪問路徑的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
詳解SpringCloud新一代網(wǎng)關(guān)Gateway
SpringCloud Gateway是Spring Cloud的一個全新項(xiàng)目,Spring 5.0+ Spring Boot 2.0和Project Reactor等技術(shù)開發(fā)的網(wǎng)關(guān),它旨在為微服務(wù)架構(gòu)提供一種簡單有效的統(tǒng)一的API路由管理方式2021-06-06
spring mvc中注解@ModelAttribute的妙用分享
這篇文章主要給大家介紹了關(guān)于spring mvc中注解@ModelAttribute妙用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-09-09
Java面向?qū)ο缶幊蹋ǚ庋b/繼承/多態(tài))實(shí)例解析
這篇文章主要介紹了Java面向?qū)ο缶幊蹋ǚ庋b/繼承/多態(tài))實(shí)例解析的相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可以了解下。2017-10-10

