@Value如何获取yml和properties配置参数
作者:stay hungry,stay you 时间:2021-12-12 13:24:01
@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
来源:https://blog.csdn.net/weixin_41987908/article/details/106982035
标签:@Value,yml,properties,参数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaWeb利用struts实现文件下载时改变文件名称
2023-10-24 11:26:53
![](https://img.aspxhome.com/file/2023/1/71621_0s.jpg)
Java Swing中JList选择事件监听器ListSelectionListener用法示例
2021-06-21 22:52:01
![](https://img.aspxhome.com/file/2023/0/61550_0s.gif)
Java面试题及答案集锦(基础题122道,代码题19道)
2023-11-25 12:36:17
java中LinkedBlockingQueue与ArrayBlockingQueue的异同
2023-08-19 02:56:49
![](https://img.aspxhome.com/file/2023/5/67185_0s.png)
SSM项目使用拦截器实现登录验证功能
2023-06-17 16:12:38
![](https://img.aspxhome.com/file/2023/0/65600_0s.png)
java多线程复制文件的实例代码
2022-03-04 04:29:54
java 中sendredirect()和forward()方法的区别
2021-11-07 18:39:28
详解WPF中的隧道路由和冒泡路由事件
2023-03-01 07:59:49
![](https://img.aspxhome.com/file/2023/8/68068_0s.png)
Spring Boot应用程序同时支持HTTP和HTTPS协议的实现方法
2022-01-10 16:50:43
![](https://img.aspxhome.com/file/2023/2/61952_0s.jpg)
spring中使用mybatis plus连接sqlserver的方法实现
2022-11-16 12:05:09
浅谈AnDroidDraw+DroidDraw实现Android程序UI设计的分析说明
2023-09-28 20:41:51
![](https://img.aspxhome.com/file/2023/1/85991_0s.jpg)
idea 实现搜索jdk中的类和包操作
2022-06-02 22:54:47
![](https://img.aspxhome.com/file/2023/9/71169_0s.jpg)
Java详细分析String类与StringBuffer和StringBuilder的使用方法
2022-04-23 15:23:30
![](https://img.aspxhome.com/file/2023/6/64946_0s.png)
Java sha1散列算法原理及代码实例
2021-06-28 05:50:22
Java多线程之彻底搞懂线程池
2023-12-18 23:54:09
![](https://img.aspxhome.com/file/2023/5/62135_0s.png)
Android Flutter中Offstage组件的使用教程详解
2023-08-20 01:01:44
Spring security如何重写Filter实现json登录
2023-09-15 13:33:31
教你如何编写简单的网络爬虫
2023-06-01 08:58:30
Java 数组高频考点分析讲解
2021-09-01 13:14:36
![](https://img.aspxhome.com/file/2023/2/61152_0s.png)
SpringMVC之@requestBody的作用及说明
2022-06-08 12:35:04