通过,注解@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;
	}

}
相关推荐
ps酷教程7 小时前
Jackson 解决没有无参构造函数的反序列化问题
java
NiceCloud喜云7 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
_日拱一卒8 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
隔窗听雨眠8 小时前
Nginx网关响应慢排查手记
java·服务器·nginx
智慧物业老杨8 小时前
智慧物业合同周期管理系统:从风险预警到智能交接的全流程数智化落地方案
java·人工智能·python
源码宝9 小时前
MES系统源码:Java8 + SpringBoot2.7 + MySQL8 + Redis,后端源码清爽易扩展
java·后端·源码·springboot·mes系统·源码二开·mes源码
JAVA社区9 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
金銀銅鐵9 小时前
[Java] 如何理解 class 文件中方法的 descriptor?
java·后端
云烟成雨TD9 小时前
Spring AI Alibaba 1.x 系列【63】AI Agent 长期记忆
java·人工智能·spring
憧憬成为java架构高手的小白10 小时前
苍穹外卖--day09
java·spring boot·百度