Skip to content

Commit cd62596

Browse files
committed
Provide default for SpringApplication main class in servlet container
By default, SpringApplication attempts to deduce the application class by looking for a main method in the stack. This does not work when the application is launched by a servlet container via SpringBootServletInitializer as there's either no main method in the stack, or the main method is that of the servlet container, rather than the application. This commit updates SpringBootServletInitializer to configure the main class of the SpringApplication that it creates to be the application's SpringBootServletInitializer subclass. This is done prior to calling configure, so the main class can still be specified by the application if required. Closes gh-3061
1 parent 01ba0f7 commit cd62596

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/context/web/SpringBootServletInitializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,6 +57,7 @@
5757
*
5858
* @author Dave Syer
5959
* @author Phillip Webb
60+
* @author Andy Wilkinson
6061
* @see #configure(SpringApplicationBuilder)
6162
*/
6263
public abstract class SpringBootServletInitializer implements WebApplicationInitializer {
@@ -84,6 +85,7 @@ public void contextInitialized(ServletContextEvent event) {
8485
protected WebApplicationContext createRootApplicationContext(
8586
ServletContext servletContext) {
8687
SpringApplicationBuilder builder = new SpringApplicationBuilder();
88+
builder.main(getClass());
8789
ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
8890
if (parent != null) {
8991
this.logger.info("Root context already created (using as parent).");

spring-boot/src/test/java/org/springframework/boot/context/web/SpringBootServletInitializerTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@
2626
import org.junit.Rule;
2727
import org.junit.Test;
2828
import org.junit.rules.ExpectedException;
29+
import org.springframework.beans.DirectFieldAccessor;
2930
import org.springframework.boot.SpringApplication;
3031
import org.springframework.boot.builder.SpringApplicationBuilder;
3132
import org.springframework.context.annotation.Configuration;
3233
import org.springframework.mock.web.MockServletContext;
3334
import org.springframework.web.context.WebApplicationContext;
3435

3536
import static org.hamcrest.Matchers.equalTo;
37+
import static org.hamcrest.Matchers.is;
3638
import static org.junit.Assert.assertThat;
3739

3840
/**
3941
* Tests for {@link SpringBootServletInitializerTests}.
4042
*
4143
* @author Phillip Webb
44+
* @author Andy Wilkinson
4245
*/
4346
public class SpringBootServletInitializerTests {
4447

@@ -72,6 +75,17 @@ public void withConfiguredSource() throws Exception {
7275
equalToSet(Config.class, ErrorPageFilter.class));
7376
}
7477

78+
@SuppressWarnings("rawtypes")
79+
@Test
80+
public void mainClassHasSensibleDefault() throws Exception {
81+
new WithConfigurationAnnotation()
82+
.createRootApplicationContext(this.servletContext);
83+
Class mainApplicationClass = (Class<?>) new DirectFieldAccessor(this.application)
84+
.getPropertyValue("mainApplicationClass");
85+
assertThat(mainApplicationClass,
86+
is(equalTo((Class) WithConfigurationAnnotation.class)));
87+
}
88+
7589
private Matcher<? super Set<Object>> equalToSet(Object... items) {
7690
Set<Object> set = new LinkedHashSet<Object>();
7791
Collections.addAll(set, items);

0 commit comments

Comments
 (0)