public static void startServer(final Class<?> springBootApplicationClass) { final String usageMessage = "Please provide the correct path to the Alexa Proxy property location with: -Dalexa.proxy.properties.location=/path/to/file"; final String alexaProxyPropertyLocation = System.getProperty("alexa.proxy.properties.location"); if (alexaProxyPropertyLocation == null) { System.out.println("No value provided for property 'alexa.proxy.properties.location'."); System.out.println(usageMessage); System.exit(-1); } final File propertiesFile = new File(alexaProxyPropertyLocation); if (!propertiesFile.exists()) { System.out.println("Property file not found: '" + alexaProxyPropertyLocation + "'."); System.out.println(usageMessage); System.exit(-1); } System.out.println("Using properties in file: " + propertiesFile.getAbsolutePath()); // Set the Spring config location file to the Alexa Proxy properties file, so it will pick up all // Spring boot config from there as well. Note: this must be done like this instead of using a @PropertySource // annotation, because otherwise the logging properties will not be picked up. They must be available // very early in the boot process, see also: https://github.com/spring-projects/spring-boot/issues/2709 System.setProperty(ConfigFileApplicationListener.CONFIG_LOCATION_PROPERTY, propertiesFile.toURI().toString()); final SpringApplication application = new SpringApplication(springBootApplicationClass); final ApplicationContext springBootContext = application.run(new String[0]); // Start a shutdown poller for graceful shutdown when needed. new ShutdownPoller(springBootContext).start(); }
@Override public Environment findOne(String config, String profile, String label) { SpringApplicationBuilder builder = new SpringApplicationBuilder( PropertyPlaceholderAutoConfiguration.class); ConfigurableEnvironment environment = getEnvironment(profile); builder.environment(environment); builder.web(WebApplicationType.NONE).bannerMode(Mode.OFF); if (!logger.isDebugEnabled()) { // Make the mini-application startup less verbose builder.logStartupInfo(false); } String[] args = getArgs(config, profile, label); // Explicitly set the listeners (to exclude logging listener which would change // log levels in the caller) builder.application() .setListeners(Arrays.asList(new ConfigFileApplicationListener())); ConfigurableApplicationContext context = builder.run(args); environment.getPropertySources().remove("profiles"); try { return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label)); } finally { context.close(); } }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { Class[] loggers = {SpringApplication.class, /*App.class,*/ ConfigFileApplicationListener.class, EndpointMBeanExporter.class, AutoConfigurationReportLoggingInitializer.class}; Logger log = (Logger) LoggerFactory.getLogger("ROOT"); log.setLevel(Level.ERROR); for (Class logger : loggers) { log = (Logger) LoggerFactory.getLogger(logger); log.setLevel(Level.ERROR); } }
@Override public void initialize(final ConfigurableApplicationContext applicationContext) { new ConfigFileApplicationListener() { public void apply() { addPropertySources(applicationContext.getEnvironment(), applicationContext); addPostProcessors(applicationContext); } }.apply(); }
private Collection<ApplicationListener<?>> getListeners() { List<ApplicationListener<?>> listeners = new ArrayList<ApplicationListener<?>>(); listeners.add(new AnsiOutputApplicationListener()); listeners.add(new ConfigFileApplicationListener()); listeners.add(new ClasspathLoggingApplicationListener()); listeners.add(new LoggingApplicationListener()); listeners.add(new RemoteUrlPropertyExtractor()); return listeners; }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { Class[] loggers = { SpringApplication.class, App.class, ConfigFileApplicationListener.class, EndpointMBeanExporter.class, AutoConfigurationReportLoggingInitializer.class }; Logger log = (Logger) LoggerFactory.getLogger("ROOT"); log.setLevel(Level.ERROR); for (Class logger : loggers) { log = (Logger) LoggerFactory.getLogger(logger); log.setLevel(Level.ERROR); } }
private Set<String> addIncludedProfilesTo(Set<String> profiles, PropertySource<?> propertySource) { if (propertySource instanceof CompositePropertySource) { for (PropertySource<?> nestedPropertySource : ((CompositePropertySource) propertySource) .getPropertySources()) { addIncludedProfilesTo(profiles, nestedPropertySource); } } else { Collections.addAll(profiles, getProfilesForValue(propertySource.getProperty( ConfigFileApplicationListener.INCLUDE_PROFILES_PROPERTY))); } return profiles; }
/** * 配置系统属性 */ private static final void configSystemProperties() { System.setProperty("spring.config.name", "application,configx"); System.setProperty(ConfigFileApplicationListener.CONFIG_LOCATION_PROPERTY, "file:../config/"); }
public int getOrder() { // +1 保证application.propertie里的内容能覆盖掉本配置文件中默认的 // 如果不想被覆盖 可以去掉 +1 或者 -1 试试 return ConfigFileApplicationListener.DEFAULT_ORDER + 1; }
@Override public int getOrder() { return ConfigFileApplicationListener.DEFAULT_ORDER + 1; }
ConfigurableApplicationContext addConfigFilesToEnvironment() { ConfigurableApplicationContext capture = null; try { StandardEnvironment environment = copyEnvironment( this.context.getEnvironment()); SpringApplicationBuilder builder = new SpringApplicationBuilder(Empty.class) .bannerMode(Mode.OFF).web(WebApplicationType.NONE) .environment(environment); // Just the listeners that affect the environment (e.g. excluding logging // listener because it has side effects) builder.application() .setListeners(Arrays.asList(new BootstrapApplicationListener(), new ConfigFileApplicationListener())); capture = builder.run(); if (environment.getPropertySources().contains(REFRESH_ARGS_PROPERTY_SOURCE)) { environment.getPropertySources().remove(REFRESH_ARGS_PROPERTY_SOURCE); } MutablePropertySources target = this.context.getEnvironment() .getPropertySources(); String targetName = null; for (PropertySource<?> source : environment.getPropertySources()) { String name = source.getName(); if (target.contains(name)) { targetName = name; } if (!this.standardSources.contains(name)) { if (target.contains(name)) { target.replace(name, source); } else { if (targetName != null) { target.addAfter(targetName, source); } else { // targetName was null so we are at the start of the list target.addFirst(source); targetName = name; } } } } } finally { ConfigurableApplicationContext closeable = capture; while (closeable != null) { try { closeable.close(); } catch (Exception e) { // Ignore; } if (closeable.getParent() instanceof ConfigurableApplicationContext) { closeable = (ConfigurableApplicationContext) closeable.getParent(); } else { break; } } } return capture; }