diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 2aec2da88d..d0110ffca3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -77,38 +77,38 @@ import org.springframework.web.context.support.StandardServletEnvironment; * Classes that can be used to bootstrap and launch a Spring application from a Java main * method. By default class will perform the following steps to bootstrap your * application: - * + * * - * + * * In most circumstances the static {@link #run(Object, String[])} method can be called * directly from your {@literal main} method to bootstrap your application: - * + * *
  * @Configuration
  * @EnableAutoConfiguration
  * public class MyApplication  {
- * 
+ *
  * // ... Bean definitions
- * 
+ *
  * public static void main(String[] args) throws Exception {
  *   SpringApplication.run(MyApplication.class, args);
  * }
  * 
- * + * *

* For more advanced configuration a {@link SpringApplication} instance can be created and * customized before being run: - * + * *

  * public static void main(String[] args) throws Exception {
  *   SpringApplication app = new SpringApplication(MyApplication.class);
@@ -116,28 +116,29 @@ import org.springframework.web.context.support.StandardServletEnvironment;
  *   app.run(args)
  * }
  * 
- * + * * {@link SpringApplication}s can read beans from a variety of different sources. It is * generally recommended that a single {@code @Configuration} class is used to bootstrap * your application, however, any of the following sources can also be used: - * + * *

*

- * + * * @author Phillip Webb * @author Dave Syer + * @author Andy Wilkinson * @see #run(Object, String[]) * @see #run(Object[], String[]) * @see #SpringApplication(Object...) @@ -151,7 +152,9 @@ public class SpringApplication { + "boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext"; private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet", - "org.springframework.web.context.ConfigurableWebApplicationContext" }; + "org.springframework.web.context.ConfigurableWebApplicationContext" }; + + private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless"; private final Log log = LogFactory.getLog(getClass()); @@ -262,7 +265,10 @@ public class SpringApplication { stopWatch.start(); ConfigurableApplicationContext context = null; - System.setProperty("java.awt.headless", Boolean.toString(this.headless)); + System.setProperty( + SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, + System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, + Boolean.toString(this.headless))); Collection runListeners = getRunListeners(args); for (SpringApplicationRunListener runListener : runListeners) { @@ -540,7 +546,7 @@ public class SpringApplication { if (this.resourceLoader != null) { if (context instanceof GenericApplicationContext) { ((GenericApplicationContext) context) - .setResourceLoader(this.resourceLoader); + .setResourceLoader(this.resourceLoader); } if (context instanceof DefaultResourceLoader) { ((DefaultResourceLoader) context).setClassLoader(this.resourceLoader @@ -573,7 +579,7 @@ public class SpringApplication { protected void logStartupInfo(boolean isRoot) { if (isRoot) { new StartupInfoLogger(this.mainApplicationClass) - .logStarting(getApplicationLog()); + .logStarting(getApplicationLog()); } } diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 23ab071b89..445c448226 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -23,6 +23,7 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -78,12 +79,15 @@ import static org.mockito.Mockito.verify; /** * Tests for {@link SpringApplication}. - * + * * @author Phillip Webb * @author Dave Syer + * @author Andy Wilkinson */ public class SpringApplicationTests { + private String headlessProperty; + @Rule public ExpectedException thrown = ExpectedException.none(); @@ -96,6 +100,23 @@ public class SpringApplicationTests { throw new IllegalStateException("Could not obtain Environment"); } + @Before + public void storeAndClearHeadlessProperty() { + this.headlessProperty = System.getProperty("java.awt.headless"); + System.clearProperty("java.awt.headless"); + } + + @After + public void reinstateHeadlessProperty() { + if (this.headlessProperty == null) { + System.clearProperty("java.awt.headless"); + } + else { + System.setProperty("java.awt.headless", this.headlessProperty); + } + + } + @After public void close() { if (this.context != null) { @@ -504,6 +525,15 @@ public class SpringApplicationTests { assertThat(System.getProperty("java.awt.headless"), equalTo("false")); } + @Test + public void headlessSystemPropertyTakesPrecedence() throws Exception { + System.setProperty("java.awt.headless", "false"); + TestSpringApplication application = new TestSpringApplication(ExampleConfig.class); + application.setWebEnvironment(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()) {