本篇内容主要讲解“@Value怎么获取yml和properties配置参数”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“@Value怎么获取yml和properties配置参数”吧!
@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”) 直接赋值
@value注解获取yml文件中的值问题
在类中使用@Value注解获取yml配置文件中的值时,需要注意:
1、yml文件中,当值为0000
这种类型的值时,需要用双引号将值引起来。
比如:
错误:key=0000
正确:key=“0000”
如果不使用双引号的话,在使用@value注解时,得到的值是0,而不是0000
2、使用@Value注解得到的是null
需要使用@Autowired进行注入,对应类需要加上@Service
到此,相信大家对“@Value怎么获取yml和properties配置参数”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!