Application.java 2.62 KB
package com.bsth;

import com.bsth.util.AppProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.core.env.ConfigurableEnvironment;

import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	public static ScheduledExecutorService mainServices = Executors.newScheduledThreadPool(10);

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	public static void main(String[] args) throws Exception {
		RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
        List<String> arguments = runtimeMxBean.getInputArguments();
        boolean normal = false;
        for (String arg : arguments) {
            if (arg.indexOf("-XX:+DisableAttachMechanism") > -1) {
                normal = true;
                break;
            }
        }

        if (normal) {
			loadProperties();
			SpringApplication.run(Application.class, args);
        }
	}

	private static void loadProperties() {
		Properties prop = new Properties();
		try (InputStream input = Application.class.getClassLoader().getResourceAsStream("application.properties")) {
			prop.load(input);
			String activeProfile = prop.getProperty("spring.profiles.active", "");
			String cacheDays = prop.getProperty("cache.days", "15");
			InputStream input1 = Application.class.getClassLoader().getResourceAsStream(String.format("application-%s.properties", activeProfile));
			prop.load(input1);
			String controlUrl = prop.getProperty("http.control.service_data_url", "");
			String secretKey = prop.getProperty("http.control.secret.key", "");
			String gpsUrl = prop.getProperty("http.gps.real.url", "");

			AppProperties.setCacheDays(Integer.parseInt(cacheDays));
			AppProperties.setControlUrl(controlUrl);
			AppProperties.setSecretKey(secretKey);
			AppProperties.setGpsUrl(gpsUrl);
		} catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}