From 456d1af443b9c3cc97f09abeda07f3137362e2c4 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 5 Oct 2018 13:22:32 -0700 Subject: [PATCH 1/2] Polish --- .../spring-boot-starters/pom.xml | 6 +- .../boot/SpringApplication.java | 63 +++---------------- .../boot/WebApplicationType.java | 53 +++++++++++++++- 3 files changed, 65 insertions(+), 57 deletions(-) diff --git a/spring-boot-project/spring-boot-starters/pom.xml b/spring-boot-project/spring-boot-starters/pom.xml index 5bf466d0354..6876c002b2b 100644 --- a/spring-boot-project/spring-boot-starters/pom.xml +++ b/spring-boot-project/spring-boot-starters/pom.xml @@ -1,5 +1,6 @@ - 4.0.0 @@ -145,6 +146,9 @@ changelog.txt + + module-info + diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 1119f38bbe8..ecb3bbac9a6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -164,12 +164,9 @@ public class SpringApplication { * The class name of application context that will be used by default for web * environments. */ - public static final String DEFAULT_WEB_CONTEXT_CLASS = "org.springframework.boot." + public static final String DEFAULT_SERVLET_WEB_CONTEXT_CLASS = "org.springframework.boot." + "web.servlet.context.AnnotationConfigServletWebServerApplicationContext"; - private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet", - "org.springframework.web.context.ConfigurableWebApplicationContext" }; - /** * The class name of application context that will be used by default for reactive web * environments. @@ -177,14 +174,6 @@ public class SpringApplication { public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework." + "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext"; - private static final String REACTIVE_WEB_ENVIRONMENT_CLASS = "org.springframework." - + "web.reactive.DispatcherHandler"; - - private static final String MVC_WEB_ENVIRONMENT_CLASS = "org.springframework." - + "web.servlet.DispatcherServlet"; - - private static final String JERSEY_WEB_ENVIRONMENT_CLASS = "org.glassfish.jersey.server.ResourceConfig"; - /** * Default banner location. */ @@ -266,27 +255,13 @@ public class SpringApplication { this.resourceLoader = resourceLoader; Assert.notNull(primarySources, "PrimarySources must not be null"); this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); - this.webApplicationType = deduceWebApplicationType(); + this.webApplicationType = WebApplicationType.deduceFromClasspath(); setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = deduceMainApplicationClass(); } - private WebApplicationType deduceWebApplicationType() { - if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null) - && !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null) - && !ClassUtils.isPresent(JERSEY_WEB_ENVIRONMENT_CLASS, null)) { - return WebApplicationType.REACTIVE; - } - for (String className : WEB_ENVIRONMENT_CLASSES) { - if (!ClassUtils.isPresent(className, null)) { - return WebApplicationType.NONE; - } - } - return WebApplicationType.SERVLET; - } - private Class deduceMainApplicationClass() { try { StackTraceElement[] stackTrace = new RuntimeException().getStackTrace(); @@ -596,7 +571,7 @@ public class SpringApplication { try { switch (this.webApplicationType) { case SERVLET: - contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS); + contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS); break; case REACTIVE: contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); @@ -1173,38 +1148,16 @@ public class SpringApplication { /** * Sets the type of Spring {@link ApplicationContext} that will be created. If not - * specified defaults to {@link #DEFAULT_WEB_CONTEXT_CLASS} for web based applications - * or {@link AnnotationConfigApplicationContext} for non web based applications. + * specified defaults to {@link #DEFAULT_SERVLET_WEB_CONTEXT_CLASS} for web based + * applications or {@link AnnotationConfigApplicationContext} for non web based + * applications. * @param applicationContextClass the context class to set */ public void setApplicationContextClass( Class applicationContextClass) { this.applicationContextClass = applicationContextClass; - this.webApplicationType = deduceWebApplicationType(applicationContextClass); - } - - private WebApplicationType deduceWebApplicationType( - Class applicationContextClass) { - if (safeIsAssignableFrom("org.springframework.web.context.WebApplicationContext", - applicationContextClass)) { - return WebApplicationType.SERVLET; - } - if (safeIsAssignableFrom( - "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext", - applicationContextClass)) { - return WebApplicationType.REACTIVE; - } - return WebApplicationType.NONE; - } - - private boolean safeIsAssignableFrom(String target, Class type) { - try { - Class targetClass = ClassUtils.forName(target, getClassLoader()); - return targetClass.isAssignableFrom(type); - } - catch (Throwable ex) { - return false; - } + this.webApplicationType = WebApplicationType + .deduceFromApplicationContext(applicationContextClass); } /** diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java index 7ca7aebd476..a80217d382b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java @@ -16,6 +16,8 @@ package org.springframework.boot; +import org.springframework.util.ClassUtils; + /** * An enumeration of possible types of web application. * @@ -41,6 +43,55 @@ public enum WebApplicationType { * The application should run as a reactive web application and should start an * embedded reactive web server. */ - REACTIVE + REACTIVE; + + private static final String[] SERVLET_INDICATOR_CLASSES = { "javax.servlet.Servlet", + "org.springframework.web.context.ConfigurableWebApplicationContext" }; + + private static final String WEBMVC_INDICATOR_CLASS = "org.springframework." + + "web.servlet.DispatcherServlet"; + + private static final String WEBFLUX_INDICATOR_CLASS = "org." + + "springframework.web.reactive.DispatcherHandler"; + + private static final String JERSEY_INDICATOR_CLASS = "org.glassfish.jersey.server.ResourceConfig"; + + private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext"; + + private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext"; + + static WebApplicationType deduceFromClasspath() { + if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) + && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null) + && !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) { + return WebApplicationType.REACTIVE; + } + for (String className : SERVLET_INDICATOR_CLASSES) { + if (!ClassUtils.isPresent(className, null)) { + return WebApplicationType.NONE; + } + } + return WebApplicationType.SERVLET; + } + + static WebApplicationType deduceFromApplicationContext( + Class applicationContextClass) { + if (isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS, applicationContextClass)) { + return WebApplicationType.SERVLET; + } + if (isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS, applicationContextClass)) { + return WebApplicationType.REACTIVE; + } + return WebApplicationType.NONE; + } + + private static boolean isAssignable(String target, Class type) { + try { + return ClassUtils.resolveClassName(target, null).isAssignableFrom(type); + } + catch (Throwable ex) { + return false; + } + } } From 6aedb69443b7216021bd3dad2ff2bb85bd83bded Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 5 Oct 2018 13:31:33 -0700 Subject: [PATCH 2/2] Update copyright year for changed files --- .../logging/ConditionEvaluationReportLoggingListenerTests.java | 2 +- ...gureTestDatabaseWithMultipleDatasourcesIntegrationTests.java | 2 +- .../boot/test/autoconfigure/jdbc/ExampleJdbcApplication.java | 2 +- ...utoConfigureTestDatabaseReplaceExplicitIntegrationTests.java | 2 +- ...ConfigureTestDatabaseReplacePropertyAnyIntegrationTests.java | 2 +- .../boot/test/autoconfigure/jooq/ExampleJooqApplication.java | 2 +- .../test/autoconfigure/orm/jpa/ExampleDataJpaApplication.java | 2 +- .../main/java/org/springframework/boot/WebApplicationType.java | 2 +- .../main/java/org/springframework/boot/lang/UsesUnsafeJava.java | 2 +- .../main/java/org/springframework/boot/lang/package-info.java | 2 +- .../boot/devtools/tests/DevToolsIntegrationTests.java | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java index 01ca0da25c2..5c7a5ef82a3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java index 2ea3b843b52..2287eeedb33 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcApplication.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcApplication.java index 37f68f9416a..e1ee1a64804 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcApplication.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java index 7a50f210c86..5c54e141b4a 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplacePropertyAnyIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplacePropertyAnyIntegrationTests.java index 3b7a2ccd3a3..b86ccef1b75 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplacePropertyAnyIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplacePropertyAnyIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/ExampleJooqApplication.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/ExampleJooqApplication.java index 25833cebd20..3a4d5797587 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/ExampleJooqApplication.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/ExampleJooqApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/ExampleDataJpaApplication.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/ExampleDataJpaApplication.java index 94395531913..8a24c106b42 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/ExampleDataJpaApplication.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/ExampleDataJpaApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java index a80217d382b..f95233d5f74 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/WebApplicationType.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/lang/UsesUnsafeJava.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/lang/UsesUnsafeJava.java index 914ff693cd6..7ecca9df421 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/lang/UsesUnsafeJava.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/lang/UsesUnsafeJava.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/lang/package-info.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/lang/package-info.java index 83349d70de8..bfbdffa7d0c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/lang/package-info.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/lang/package-info.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-devtools-tests/src/test/java/org/springframework/boot/devtools/tests/DevToolsIntegrationTests.java b/spring-boot-tests/spring-boot-integration-tests/spring-boot-devtools-tests/src/test/java/org/springframework/boot/devtools/tests/DevToolsIntegrationTests.java index 9a141dd7e09..199d3e6328b 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-devtools-tests/src/test/java/org/springframework/boot/devtools/tests/DevToolsIntegrationTests.java +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-devtools-tests/src/test/java/org/springframework/boot/devtools/tests/DevToolsIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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.