mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Change semantics of spring.main.sources slightly
Now the environment overrides aare **added** to the sources provided in SpringApplication.run(). This avoids some potential surprises where setSources() would have been silently ignored. [#54185750]
This commit is contained in:
parent
25f31b6358
commit
3fda19af58
@ -135,7 +135,9 @@ public class SpringApplication {
|
|||||||
|
|
||||||
private final Log log = LogFactory.getLog(getClass());
|
private final Log log = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
private Set<Object> sources = new LinkedHashSet<Object>();
|
private Set<Object> defaultSources = new LinkedHashSet<Object>();
|
||||||
|
|
||||||
|
private Set<Object> additionalSources = new LinkedHashSet<Object>();
|
||||||
|
|
||||||
private Class<?> mainApplicationClass;
|
private Class<?> mainApplicationClass;
|
||||||
|
|
||||||
@ -161,8 +163,6 @@ public class SpringApplication {
|
|||||||
|
|
||||||
private String[] defaultCommandLineArgs;
|
private String[] defaultCommandLineArgs;
|
||||||
|
|
||||||
private boolean sourcesInitialized = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crate a new {@link SpringApplication} instance. The application context will load
|
* Crate a new {@link SpringApplication} instance. The application context will load
|
||||||
* beans from the specified sources (see {@link SpringApplication class-level}
|
* beans from the specified sources (see {@link SpringApplication class-level}
|
||||||
@ -193,8 +193,7 @@ public class SpringApplication {
|
|||||||
|
|
||||||
private void initialize(Object[] sources) {
|
private void initialize(Object[] sources) {
|
||||||
if (sources != null && sources.length > 0) {
|
if (sources != null && sources.length > 0) {
|
||||||
this.sourcesInitialized = true;
|
this.additionalSources.addAll(Arrays.asList(sources));
|
||||||
this.sources.addAll(Arrays.asList(sources));
|
|
||||||
}
|
}
|
||||||
this.webEnvironment = deduceWebEnvironment();
|
this.webEnvironment = deduceWebEnvironment();
|
||||||
this.initializers = new ArrayList<ApplicationContextInitializer<?>>();
|
this.initializers = new ArrayList<ApplicationContextInitializer<?>>();
|
||||||
@ -249,7 +248,8 @@ public class SpringApplication {
|
|||||||
|
|
||||||
// Call all remaining initializers
|
// Call all remaining initializers
|
||||||
callEnvironmentAwareSpringApplicationInitializers(environment);
|
callEnvironmentAwareSpringApplicationInitializers(environment);
|
||||||
Assert.notEmpty(this.sources, "Sources must not be empty");
|
Set<Object> sources = assembleSources();
|
||||||
|
Assert.notEmpty(sources, "Sources must not be empty");
|
||||||
if (this.showBanner) {
|
if (this.showBanner) {
|
||||||
printBanner();
|
printBanner();
|
||||||
}
|
}
|
||||||
@ -267,12 +267,19 @@ public class SpringApplication {
|
|||||||
if (this.logStartupInfo) {
|
if (this.logStartupInfo) {
|
||||||
logStartupInfo();
|
logStartupInfo();
|
||||||
}
|
}
|
||||||
load(context, this.sources.toArray(new Object[this.sources.size()]));
|
load(context, sources.toArray(new Object[sources.size()]));
|
||||||
refresh(context);
|
refresh(context);
|
||||||
runCommandLineRunners(context, args);
|
runCommandLineRunners(context, args);
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Set<Object> assembleSources() {
|
||||||
|
LinkedHashSet<Object> sources = new LinkedHashSet<Object>();
|
||||||
|
sources.addAll(this.defaultSources);
|
||||||
|
sources.addAll(this.additionalSources);
|
||||||
|
return sources;
|
||||||
|
}
|
||||||
|
|
||||||
private void callNonEnvironmentAwareSpringApplicationInitializers() {
|
private void callNonEnvironmentAwareSpringApplicationInitializers() {
|
||||||
for (ApplicationContextInitializer<?> initializer : this.initializers) {
|
for (ApplicationContextInitializer<?> initializer : this.initializers) {
|
||||||
if (initializer instanceof SpringApplicationInitializer
|
if (initializer instanceof SpringApplicationInitializer
|
||||||
@ -354,7 +361,7 @@ public class SpringApplication {
|
|||||||
Log applicationLog = getApplicationLog();
|
Log applicationLog = getApplicationLog();
|
||||||
new StartupInfoLogger(this.mainApplicationClass).log(applicationLog);
|
new StartupInfoLogger(this.mainApplicationClass).log(applicationLog);
|
||||||
if (applicationLog.isDebugEnabled()) {
|
if (applicationLog.isDebugEnabled()) {
|
||||||
applicationLog.debug("Sources: " + this.sources);
|
applicationLog.debug("Sources: " + this.defaultSources);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -561,13 +568,13 @@ public class SpringApplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a mutable set of the sources that will be used to create an
|
* Returns a mutable set of the sources that will be added to an ApplicationContext
|
||||||
* ApplicationContext when {@link #run(String...)} is called.
|
* when {@link #run(String...)} is called.
|
||||||
* @return the sources the application sources.
|
* @return the sources the application sources.
|
||||||
* @see #SpringApplication(Object...)
|
* @see #SpringApplication(Object...)
|
||||||
*/
|
*/
|
||||||
public Set<Object> getSources() {
|
public Set<Object> getSources() {
|
||||||
return this.sources;
|
return this.defaultSources;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -579,11 +586,8 @@ public class SpringApplication {
|
|||||||
* @see #SpringApplication(Object...)
|
* @see #SpringApplication(Object...)
|
||||||
*/
|
*/
|
||||||
public void setSources(Set<Object> sources) {
|
public void setSources(Set<Object> sources) {
|
||||||
if (this.sourcesInitialized) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Assert.notNull(sources, "Sources must not be null");
|
Assert.notNull(sources, "Sources must not be null");
|
||||||
this.sources = new LinkedHashSet<Object>(sources);
|
this.defaultSources = new LinkedHashSet<Object>(sources);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.springframework.boot;
|
package org.springframework.boot;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@ -47,6 +48,7 @@ import org.springframework.core.env.StandardEnvironment;
|
|||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
import org.springframework.mock.web.MockServletContext;
|
import org.springframework.mock.web.MockServletContext;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
@ -268,7 +270,10 @@ public class SpringApplicationTests {
|
|||||||
application.setWebEnvironment(false);
|
application.setWebEnvironment(false);
|
||||||
application.setUseMockLoader(true);
|
application.setUseMockLoader(true);
|
||||||
application.run();
|
application.run();
|
||||||
assertThat(application.getSources().toArray(), equalTo(sources));
|
@SuppressWarnings("unchecked")
|
||||||
|
Set<Object> additionalSources = (Set<Object>) ReflectionTestUtils.getField(
|
||||||
|
application, "additionalSources");
|
||||||
|
assertThat(additionalSources.toArray(), equalTo(sources));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user