All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.xtazxz.base.common.component.BaseApplication Maven / Gradle / Ivy

The newest version!
package io.github.xtazxz.base.common.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

@Slf4j
public class BaseApplication {

  public static final String ENV_SPRING_PROFILES_ACTIVE = "spring_profiles_active";
  public static final String PROPERTY_SPRING_PROFILES_ACTIVE = "spring.profiles.active";

  static {
    // 默认激活local的profile
    String env = System.getenv(ENV_SPRING_PROFILES_ACTIVE);
    String property = System.getProperty(PROPERTY_SPRING_PROFILES_ACTIVE);
    if ((env == null || env.trim().isEmpty())
        && (property == null || property.trim().isEmpty())) {
      System.setProperty(PROPERTY_SPRING_PROFILES_ACTIVE, "local");
    }
  }

  public static void run(Class applicationClass, String... args) {
    ConfigurableApplicationContext context = SpringApplication.run(applicationClass, args);
    Environment env = context.getEnvironment();
    String startMsg = """

        ----------------------------------------------------------
        Application '{}' is running!
        Doc: http://localhost:{}{}/doc.html
        ----------------------------------------------------------
        """;
    String applicationName = env.getProperty("spring.application.name");
    String serverPort = env.getProperty("server.port");
    String contextPath = env.getProperty("server.servlet.context-path");
    if (serverPort == null) {
      serverPort = "8080";
    }
    if (contextPath == null) {
      contextPath = "";
    }
    log.info(startMsg, applicationName, serverPort, contextPath);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy