溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

@Value怎么獲取yml和properties配置參數(shù)

發(fā)布時(shí)間:2021-07-07 10:42:21 來源:億速云 閱讀:330 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“@Value怎么獲取yml和properties配置參數(shù)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“@Value怎么獲取yml和properties配置參數(shù)”吧!

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

@Value怎么獲取yml和properties配置參數(shù)

Yml:

#定時(shí)任務(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),注冊(cè)到微服務(wù)的/api/server接口,完成注冊(cè)動(dòng)作
        executor:          
          appname: af_job   #執(zhí)行器名稱,要求務(wù)必唯一
          ip: 10.21.126.237  #執(zhí)行器IP [選填]:默認(rèn)為空表示自動(dòng)獲取IP,多網(wǎng)卡時(shí)可手動(dò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怎么獲取yml和properties配置參數(shù) @Value怎么獲取yml和properties配置參數(shù)

賦值:

@Value(“true”) 直接賦值

@value注解獲取yml文件中的值問題

在類中使用@Value注解獲取yml配置文件中的值時(shí),需要注意:

1、yml文件中,當(dāng)值為0000

這種類型的值時(shí),需要用雙引號(hào)將值引起來。

比如:

錯(cuò)誤:key=0000

正確:key=“0000”

如果不使用雙引號(hào)的話,在使用@value注解時(shí),得到的值是0,而不是0000

2、使用@Value注解得到的是null

需要使用@Autowired進(jìn)行注入,對(duì)應(yīng)類需要加上@Service

到此,相信大家對(duì)“@Value怎么獲取yml和properties配置參數(shù)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI