package com.qianwen.core.launch.props; import java.util.HashMap; import java.util.Map; import java.util.Objects; import com.qianwen.core.launch.constant.NacosConstant; import com.qianwen.core.launch.constant.TokenConstant; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; import org.springframework.core.env.EnvironmentCapable; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @ConfigurationProperties(NacosConstant.NACOS_CONFIG_PREFIX) public class BladeProperties implements EnvironmentAware, EnvironmentCapable { @Nullable private Environment environment; private final Map prop = new HashMap<>(); public Map getProp() { return this.prop; } @Nullable public String get(String key) { return get(key, null); } @Nullable public String get(String key, @Nullable String defaultValue) { String value = this.prop.get(key); if (value == null) { return defaultValue; } return value; } @Nullable public Integer getInt(String key) { return getInt(key, null); } @Nullable public Integer getInt(String key, @Nullable Integer defaultValue) { String value = this.prop.get(key); if (value != null) { return Integer.valueOf(value.trim()); } return defaultValue; } @Nullable public Long getLong(String key) { return getLong(key, null); } @Nullable public Long getLong(String key, @Nullable Long defaultValue) { String value = this.prop.get(key); if (value != null) { return Long.valueOf(value.trim()); } return defaultValue; } @Nullable public Boolean getBoolean(String key) { return getBoolean(key, null); } @Nullable public Boolean getBoolean(String key, @Nullable Boolean defaultValue) { String value = this.prop.get(key); if (value != null) { return Boolean.valueOf(Boolean.parseBoolean(value.toLowerCase().trim())); } return defaultValue; } @Nullable public Double getDouble(String key) { return getDouble(key, null); } @Nullable public Double getDouble(String key, @Nullable Double defaultValue) { String value = this.prop.get(key); if (value != null) { return Double.valueOf(Double.parseDouble(value.trim())); } return defaultValue; } public boolean containsKey(String key) { return this.prop.containsKey(key); } public String getEnv() { Objects.requireNonNull(this.environment, "Spring boot 环境下 Environment 不可能为null"); String env = this.environment.getProperty("blade.env"); Assert.notNull(env, "请使用 BladeApplication 启动..."); return env; } public String getName() { Objects.requireNonNull(this.environment, "Spring boot 环境下 Environment 不可能为null"); return this.environment.getProperty("spring.application.name", this.environment.getProperty("blade.name", TokenConstant.DEFAULT_AVATAR)); } public void setEnvironment(Environment environment) { this.environment = environment; } public Environment getEnvironment() { Objects.requireNonNull(this.environment, "Spring boot 环境下 Environment 不可能为null"); return this.environment; } }