Skip to content

Commit e9c69aa

Browse files
committed
Honour existing java.awt.headless configuration
Previously, SpringApplication would set the java.awt.headless system property even if it had already been set. This commit updates SpringApplication to honour any existing configuration when setting the property. Fixes #1189
1 parent 53be0f8 commit e9c69aa

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,67 +77,68 @@
7777
* Classes that can be used to bootstrap and launch a Spring application from a Java main
7878
* method. By default class will perform the following steps to bootstrap your
7979
* application:
80-
*
80+
*
8181
* <ul>
8282
* <li>Create an appropriate {@link ApplicationContext} instance (depending on your
8383
* classpath)</li>
84-
*
84+
*
8585
* <li>Register a {@link CommandLinePropertySource} to expose command line arguments as
8686
* Spring properties</li>
87-
*
87+
*
8888
* <li>Refresh the application context, loading all singleton beans</li>
89-
*
89+
*
9090
* <li>Trigger any {@link CommandLineRunner} beans</li>
9191
* </ul>
92-
*
92+
*
9393
* In most circumstances the static {@link #run(Object, String[])} method can be called
9494
* directly from your {@literal main} method to bootstrap your application:
95-
*
95+
*
9696
* <pre class="code">
9797
* &#064;Configuration
9898
* &#064;EnableAutoConfiguration
9999
* public class MyApplication {
100-
*
100+
*
101101
* // ... Bean definitions
102-
*
102+
*
103103
* public static void main(String[] args) throws Exception {
104104
* SpringApplication.run(MyApplication.class, args);
105105
* }
106106
* </pre>
107-
*
107+
*
108108
* <p>
109109
* For more advanced configuration a {@link SpringApplication} instance can be created and
110110
* customized before being run:
111-
*
111+
*
112112
* <pre class="code">
113113
* public static void main(String[] args) throws Exception {
114114
* SpringApplication app = new SpringApplication(MyApplication.class);
115115
* // ... customize app settings here
116116
* app.run(args)
117117
* }
118118
* </pre>
119-
*
119+
*
120120
* {@link SpringApplication}s can read beans from a variety of different sources. It is
121121
* generally recommended that a single {@code @Configuration} class is used to bootstrap
122122
* your application, however, any of the following sources can also be used:
123-
*
123+
*
124124
* <p>
125125
* <ul>
126126
* <li>{@link Class} - A Java class to be loaded by {@link AnnotatedBeanDefinitionReader}</li>
127-
*
127+
*
128128
* <li>{@link Resource} - An XML resource to be loaded by {@link XmlBeanDefinitionReader},
129129
* or a groovy script to be loaded by {@link GroovyBeanDefinitionReader}</li>
130-
*
130+
*
131131
* <li>{@link Package} - A Java package to be scanned by
132132
* {@link ClassPathBeanDefinitionScanner}</li>
133-
*
133+
*
134134
* <li>{@link CharSequence} - A class name, resource handle or package name to loaded as
135135
* appropriate. If the {@link CharSequence} cannot be resolved to class and does not
136136
* resolve to a {@link Resource} that exists it will be considered a {@link Package}.</li>
137137
* </ul>
138-
*
138+
*
139139
* @author Phillip Webb
140140
* @author Dave Syer
141+
* @author Andy Wilkinson
141142
* @see #run(Object, String[])
142143
* @see #run(Object[], String[])
143144
* @see #SpringApplication(Object...)
@@ -151,7 +152,9 @@ public class SpringApplication {
151152
+ "boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext";
152153

153154
private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet",
154-
"org.springframework.web.context.ConfigurableWebApplicationContext" };
155+
"org.springframework.web.context.ConfigurableWebApplicationContext" };
156+
157+
private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless";
155158

156159
private final Log log = LogFactory.getLog(getClass());
157160

@@ -262,7 +265,10 @@ public ConfigurableApplicationContext run(String... args) {
262265
stopWatch.start();
263266
ConfigurableApplicationContext context = null;
264267

265-
System.setProperty("java.awt.headless", Boolean.toString(this.headless));
268+
System.setProperty(
269+
SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
270+
System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
271+
Boolean.toString(this.headless)));
266272

267273
Collection<SpringApplicationRunListener> runListeners = getRunListeners(args);
268274
for (SpringApplicationRunListener runListener : runListeners) {
@@ -540,7 +546,7 @@ protected void postProcessApplicationContext(ConfigurableApplicationContext cont
540546
if (this.resourceLoader != null) {
541547
if (context instanceof GenericApplicationContext) {
542548
((GenericApplicationContext) context)
543-
.setResourceLoader(this.resourceLoader);
549+
.setResourceLoader(this.resourceLoader);
544550
}
545551
if (context instanceof DefaultResourceLoader) {
546552
((DefaultResourceLoader) context).setClassLoader(this.resourceLoader
@@ -573,7 +579,7 @@ protected void applyInitializers(ConfigurableApplicationContext context) {
573579
protected void logStartupInfo(boolean isRoot) {
574580
if (isRoot) {
575581
new StartupInfoLogger(this.mainApplicationClass)
576-
.logStarting(getApplicationLog());
582+
.logStarting(getApplicationLog());
577583
}
578584
}
579585

spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.concurrent.atomic.AtomicReference;
2424

2525
import org.junit.After;
26+
import org.junit.Before;
2627
import org.junit.Rule;
2728
import org.junit.Test;
2829
import org.junit.rules.ExpectedException;
@@ -78,12 +79,15 @@
7879

7980
/**
8081
* Tests for {@link SpringApplication}.
81-
*
82+
*
8283
* @author Phillip Webb
8384
* @author Dave Syer
85+
* @author Andy Wilkinson
8486
*/
8587
public class SpringApplicationTests {
8688

89+
private String headlessProperty;
90+
8791
@Rule
8892
public ExpectedException thrown = ExpectedException.none();
8993

@@ -96,6 +100,23 @@ private Environment getEnvironment() {
96100
throw new IllegalStateException("Could not obtain Environment");
97101
}
98102

103+
@Before
104+
public void storeAndClearHeadlessProperty() {
105+
this.headlessProperty = System.getProperty("java.awt.headless");
106+
System.clearProperty("java.awt.headless");
107+
}
108+
109+
@After
110+
public void reinstateHeadlessProperty() {
111+
if (this.headlessProperty == null) {
112+
System.clearProperty("java.awt.headless");
113+
}
114+
else {
115+
System.setProperty("java.awt.headless", this.headlessProperty);
116+
}
117+
118+
}
119+
99120
@After
100121
public void close() {
101122
if (this.context != null) {
@@ -504,6 +525,15 @@ public void headlessFalse() throws Exception {
504525
assertThat(System.getProperty("java.awt.headless"), equalTo("false"));
505526
}
506527

528+
@Test
529+
public void headlessSystemPropertyTakesPrecedence() throws Exception {
530+
System.setProperty("java.awt.headless", "false");
531+
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
532+
application.setWebEnvironment(false);
533+
application.run();
534+
assertThat(System.getProperty("java.awt.headless"), equalTo("false"));
535+
}
536+
507537
private boolean hasPropertySource(ConfigurableEnvironment environment,
508538
Class<?> propertySourceClass, String name) {
509539
for (PropertySource<?> source : environment.getPropertySources()) {

0 commit comments

Comments
 (0)