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

}
相关推荐
皮皮林55144 分钟前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
华仔啊6 小时前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing6 小时前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠1 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840821 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide1 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家1 天前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java
花花无缺1 天前
搞懂new 关键字(构造函数)和 .builder() 模式(建造者模式)创建对象
java
用户908324602731 天前
Spring Boot + MyBatis-Plus 多租户实战:从数据隔离到权限控制的完整方案
java·后端
桦说编程1 天前
实战分析 ConcurrentHashMap.computeIfAbsent 的锁冲突问题
java·后端·性能优化