配置的获取

方法一:

import org.springframework.beans.factory.annotation.Value;
主要是利用 @Value(“${spring.profiles.active}”)的注解

配置文件的编写(bootstrap.yml或者其他配置yaml文件):
在这里插入图片描述

配置代码的编写:

@Data
@Component
public class RemoteLabelStudioConfig {
    // 获取区分环境配置
    @Value("${spring.profiles.active}")
    private String active;

    public String getActive(){
        if (!StrUtil.isNotBlank(active)){
            throw new ServiceException("当前环境配置获取失败");
        }
        return this.active;
    }
}

使用的时候比如在Service层注入此类,然后直接调用即可

public void test() {
        String active = remoteLabelStudioConfig.getActive();
        log.info(active);// xxx
    }

其他配置同样的方法。

方法二:

yaml文件中:

ai-user:
  default-password: AI1234

配置文件类:


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "ai-user")
@Data
public class AiUserModuleConfig {
    private String defaultPassword;
    private String grafana;
}

使用 注入 调用

aiUserModuleConfig.getDefaultPassword()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值