田源
2025-04-03 9b4433fddf5b401edb0aace8a404ac733b122702
Source/BladeX-Tool/blade-core-launch/src/main/java/org/springblade/core/launch/props/BladeProperties.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,226 @@
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill åº„骞 (smallchill@163.com)
 */
package org.springblade.core.launch.props;
import lombok.Getter;
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;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
 * é…ç½®æ–‡ä»¶
 *
 * @author Chill
 */
@ConfigurationProperties("blade")
public class BladeProperties implements EnvironmentAware, EnvironmentCapable {
   @Nullable
   private Environment environment;
   /**
    * è£…载自定义配置blade.prop.xxx
    */
   @Getter
   private final Map<String, String> prop = new HashMap<>();
   /**
    * èŽ·å–é…ç½®
    *
    * @param key key
    * @return value
    */
   @Nullable
   public String get(String key) {
      return get(key, null);
   }
   /**
    * èŽ·å–é…ç½®
    *
    * @param key          key
    * @param defaultValue é»˜è®¤å€¼
    * @return value
    */
   @Nullable
   public String get(String key, @Nullable String defaultValue) {
      String value = prop.get(key);
      if (value == null) {
         return defaultValue;
      }
      return value;
   }
   /**
    * èŽ·å–é…ç½®
    *
    * @param key key
    * @return int value
    */
   @Nullable
   public Integer getInt(String key) {
      return getInt(key, null);
   }
   /**
    * èŽ·å–é…ç½®
    *
    * @param key          key
    * @param defaultValue é»˜è®¤å€¼
    * @return int value
    */
   @Nullable
   public Integer getInt(String key, @Nullable Integer defaultValue) {
      String value = prop.get(key);
      if (value != null) {
         return Integer.valueOf(value.trim());
      }
      return defaultValue;
   }
   /**
    * èŽ·å–é…ç½®
    *
    * @param key key
    * @return long value
    */
   @Nullable
   public Long getLong(String key) {
      return getLong(key, null);
   }
   /**
    * èŽ·å–é…ç½®
    *
    * @param key          key
    * @param defaultValue é»˜è®¤å€¼
    * @return long value
    */
   @Nullable
   public Long getLong(String key, @Nullable Long defaultValue) {
      String value = prop.get(key);
      if (value != null) {
         return Long.valueOf(value.trim());
      }
      return defaultValue;
   }
   /**
    * èŽ·å–é…ç½®
    *
    * @param key key
    * @return Boolean value
    */
   @Nullable
   public Boolean getBoolean(String key) {
      return getBoolean(key, null);
   }
   /**
    * èŽ·å–é…ç½®
    *
    * @param key          key
    * @param defaultValue é»˜è®¤å€¼
    * @return Boolean value
    */
   @Nullable
   public Boolean getBoolean(String key, @Nullable Boolean defaultValue) {
      String value = prop.get(key);
      if (value != null) {
         value = value.toLowerCase().trim();
         return Boolean.parseBoolean(value);
      }
      return defaultValue;
   }
   /**
    * èŽ·å–é…ç½®
    *
    * @param key key
    * @return double value
    */
   @Nullable
   public Double getDouble(String key) {
      return getDouble(key, null);
   }
   /**
    * èŽ·å–é…ç½®
    *
    * @param key          key
    * @param defaultValue é»˜è®¤å€¼
    * @return double value
    */
   @Nullable
   public Double getDouble(String key, @Nullable Double defaultValue) {
      String value = prop.get(key);
      if (value != null) {
         return Double.parseDouble(value.trim());
      }
      return defaultValue;
   }
   /**
    * åˆ¤æ–­æ˜¯å¦å­˜åœ¨key
    *
    * @param key prop key
    * @return boolean
    */
   public boolean containsKey(String key) {
      return prop.containsKey(key);
   }
   /**
    * çŽ¯å¢ƒï¼Œæ–¹ä¾¿åœ¨ä»£ç ä¸­èŽ·å–
    *
    * @return çŽ¯å¢ƒ env
    */
   public String getEnv() {
      Objects.requireNonNull(environment, "Spring boot çŽ¯å¢ƒä¸‹ Environment ä¸å¯èƒ½ä¸ºnull");
      String env = environment.getProperty("blade.env");
      Assert.notNull(env, "请使用 BladeApplication å¯åЍ...");
      return env;
   }
   /**
    * åº”用名称${spring.application.name}
    *
    * @return åº”用名
    */
   public String getName() {
      Objects.requireNonNull(environment, "Spring boot çŽ¯å¢ƒä¸‹ Environment ä¸å¯èƒ½ä¸ºnull");
      return environment.getProperty("spring.application.name", environment.getProperty("blade.name", ""));
   }
   @Override
   public void setEnvironment(Environment environment) {
      this.environment = environment;
   }
   @Override
   public Environment getEnvironment() {
      Objects.requireNonNull(environment, "Spring boot çŽ¯å¢ƒä¸‹ Environment ä¸å¯èƒ½ä¸ºnull");
      return this.environment;
   }
}