@Value获取yml和properties配置参数

在这里插入图片描述
Yml:


#定时任务配置    
application:  
    xxl: 
      job: 
        enabled: true
        admin: 
          addresses: http:///yusp-job-admin/  #127.0.0.1:8080指网关ip:port,yusp-job-admin为调度中心服务名称。通过网关,注册到微服务的/api/server接口,完成注册动作
        executor:          
          appname: af_job   #执行器名称,要求务必唯一
          ip: 10.21.126.237  #执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP
          port: 9097          #调度中心给微服务发送任务,通过此端口发送指令
          logpath: D:/temp    #执行器日志文件路径
          logretentiondays: 3  # 本地日志保存天数,-1为永远保存

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”) 直接赋值

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章