方法一:
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()