Support 'headless' applications

Update SpringApplication to run by default in 'headless' mode. This
prevents the AWT system from creating a Java icon (for example in the
OSX dock).

Also update builds to run tests in 'headless' mode.
This commit is contained in:
Phillip Webb 2014-01-13 22:41:17 -08:00
parent 0e413c3b48
commit e3b352e0b5
6 changed files with 63 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -90,6 +90,7 @@ public class SpringCli {
* @return a return status code (non boot is used to indicate an error)
*/
public int runAndHandleErrors(String... args) {
System.setProperty("java.awt.headless", Boolean.toString(true));
String[] argsWithoutDebugFlags = removeDebugFlags(args);
boolean debug = argsWithoutDebugFlags.length != args.length;
try {

View File

@ -359,6 +359,7 @@
</excludes>
<systemPropertyVariables>
<java.security.egd>file:/dev/./urandom</java.security.egd>
<java.awt.headless>true</java.awt.headless>
</systemPropertyVariables>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
</configuration>

View File

@ -72,6 +72,21 @@
</additionalConfig>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
<systemPropertyVariables>
<java.security.egd>file:/dev/./urandom</java.security.egd>
<java.awt.headless>true</java.awt.headless>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<repositories>

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -177,6 +177,8 @@ public class SpringApplication {
private boolean webEnvironment;
private boolean headless = true;
private Set<ApplicationContextInitializer<?>> initializers;
private Set<ApplicationListener<?>> listeners;
@ -305,6 +307,8 @@ public class SpringApplication {
stopWatch.start();
ConfigurableApplicationContext context = null;
System.setProperty("java.awt.headless", Boolean.toString(this.headless));
ApplicationEventMulticaster multicaster = createApplicationEventMulticaster();
try {
Set<Object> sources = getSources();
@ -673,6 +677,15 @@ public class SpringApplication {
this.webEnvironment = webEnvironment;
}
/**
* Sets if the application is headless and should not instantiate AWT. Defaults to
* {@code true} to prevent java icons appearing.
* @param headless if the application is headless
*/
public void setHeadless(boolean headless) {
this.headless = headless;
}
/**
* Sets if the Spring banner should be displayed when the application runs. Defaults
* to {@code true}.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.builder;
import java.util.Arrays;
@ -59,7 +60,6 @@ import org.springframework.core.io.ResourceLoader;
* SpringApplication instead.
*
* @author Dave Syer
*
*/
public class SpringApplicationBuilder {
@ -307,6 +307,17 @@ public class SpringApplicationBuilder {
return this;
}
/**
* Sets if the application is headless and should not instantiate AWT. Defaults to
* {@code true} to prevent java icons appearing.
* @param headless if the application is headless
* @return the current builder
*/
public SpringApplicationBuilder headless(boolean headless) {
this.application.setHeadless(headless);
return this;
}
/**
* Fixes the main application class that is used to anchor the startup messages.
*

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -398,6 +398,23 @@ public class SpringApplicationTests {
verify(applicationContext.getApplicationContext()).registerShutdownHook();
}
@Test
public void headless() throws Exception {
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
application.run();
assertThat(System.getProperty("java.awt.headless"), equalTo("true"));
}
@Test
public void headlessFalse() throws Exception {
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
application.setHeadless(false);
application.run();
assertThat(System.getProperty("java.awt.headless"), equalTo("false"));
}
private boolean hasPropertySource(ConfigurableEnvironment environment,
Class<?> propertySourceClass, String name) {
for (PropertySource<?> source : environment.getPropertySources()) {