通过,注解@value,读取配置文件中的数据(并设置默认值)

1.定义配置类

java 复制代码
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
 * lecheng云相关配置
 */
@Component
@Data
@RefreshScope
public class LeChangeConfig {
    //读取配置文件中的值,并设置默认值.(配置文件如果有值,取配置文件中的值)
    @Value("${leChange.url:https://xxxxx/openapi/}")
    private String url;

    @Value("${leChange.appId:lcxxxxxxeb6}")
    private String appId;

    @Value("${leChange.appSecret:e7xxxxxxxxxxxx6e}")
    private String appSecret;
}
2.配置文件中设置值
java 复制代码
#配置文件中设置的值
leChange:
  url: https://xxxxxxxxx/openapi/
  appId: lc0xxxxxxxxxb6
  appSecret: e7xxxxxxxxx6e
3.应用该配置
java 复制代码
@Service
@Slf4j
@AllArgsConstructor
public class LeChangeDeviceServiceImpl implements LeChangeDeviceService {

	private final LeChangeConfig leChangeConfig;

	@Override
	public String getLeChangeAccessToken(){
        //注入配置类,获取值
		String url = leChangeConfig.getUrl();
		return token;
	}

}
相关推荐
budingxiaomoli3 小时前
Spring IoC &DI
java·spring·ioc·di
Spider Cat 蜘蛛猫3 小时前
Springboot SSO系统设计文档
java·spring boot·后端
未若君雅裁3 小时前
MySQL高可用与扩展-主从复制读写分离分库分表
java·数据库·mysql
学习中.........4 小时前
从扰动函数的变化,感受红黑树带来的性能提升
java
计算机安禾4 小时前
【c++面向对象编程】第24篇:类型转换运算符:自定义隐式转换与explicit
java·c++·算法
weixin199701080165 小时前
【保姆级教程】淘宝/天猫商品详情 API(item_get)接入指南:Python/Java/PHP 调用示例与 JSON 返回值解析
java·python·php
环流_5 小时前
redis核心数据类型在java中的操作
java·数据库·redis
雨辰AI5 小时前
SpringBoot3 项目国产化改造完整流程|从 MySQL 到人大金仓落地
java·数据库·后端·mysql·政务
带刺的坐椅5 小时前
Java 流程编排新范式 Solon Flow:一个引擎,七种节点,覆盖规则/任务/工作流/AI 编排全场景
java·spring·ai·solon·flow
知彼解己5 小时前
Arthas:Java生产环境问题排查利器,从入门到实战
java