Polish
This commit is contained in:
parent
f6a341e79f
commit
8ff5ce3528
|
|
@ -362,40 +362,6 @@ public class SpringApplication {
|
|||
|
||||
}
|
||||
|
||||
protected void handleFailure(ConfigurableApplicationContext context,
|
||||
ApplicationEventMulticaster multicaster, Throwable exception, String... args) {
|
||||
try {
|
||||
multicaster.multicastEvent(new ApplicationFailedEvent(this, args, context,
|
||||
exception));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// We don't want to fail here and mask the original exception
|
||||
if (this.log.isDebugEnabled()) {
|
||||
this.log.error("Error handling failed", ex);
|
||||
}
|
||||
else {
|
||||
this.log.warn("Error handling failed (" + ex.getMessage() == null ? "no error message"
|
||||
: ex.getMessage() + ")");
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void registerApplicationEventMulticaster(
|
||||
ConfigurableApplicationContext context,
|
||||
ApplicationEventMulticaster multicaster) {
|
||||
context.getBeanFactory().registerSingleton(
|
||||
AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
|
||||
multicaster);
|
||||
if (multicaster instanceof BeanFactoryAware) {
|
||||
((BeanFactoryAware) multicaster).setBeanFactory(context.getBeanFactory());
|
||||
}
|
||||
}
|
||||
|
||||
private ApplicationEventMulticaster createApplicationEventMulticaster() {
|
||||
ApplicationEventMulticaster multicaster = new SpringApplicationEventMulticaster();
|
||||
for (ApplicationListener<?> listener : getListeners()) {
|
||||
|
|
@ -404,10 +370,6 @@ public class SpringApplication {
|
|||
return multicaster;
|
||||
}
|
||||
|
||||
private void afterRefresh(ConfigurableApplicationContext context, String[] args) {
|
||||
runCommandLineRunners(context, args);
|
||||
}
|
||||
|
||||
private ConfigurableEnvironment getOrCreateEnvironment() {
|
||||
if (this.environment != null) {
|
||||
return this.environment;
|
||||
|
|
@ -464,6 +426,70 @@ public class SpringApplication {
|
|||
Banner.write(System.out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strategy method used to create the {@link ApplicationContext}. By default this
|
||||
* method will respect any explicitly set application context or application context
|
||||
* class before falling back to a suitable default.
|
||||
* @return the application context (not yet refreshed)
|
||||
* @see #setApplicationContextClass(Class)
|
||||
*/
|
||||
protected ConfigurableApplicationContext createApplicationContext() {
|
||||
Class<?> contextClass = this.applicationContextClass;
|
||||
if (contextClass == null) {
|
||||
try {
|
||||
contextClass = Class
|
||||
.forName(this.webEnvironment ? DEFAULT_WEB_CONTEXT_CLASS
|
||||
: DEFAULT_CONTEXT_CLASS);
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
throw new IllegalStateException(
|
||||
"Unable create a default ApplicationContext, "
|
||||
+ "please specify an ApplicationContextClass", ex);
|
||||
}
|
||||
}
|
||||
return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass);
|
||||
}
|
||||
|
||||
private void registerApplicationEventMulticaster(
|
||||
ConfigurableApplicationContext context,
|
||||
ApplicationEventMulticaster multicaster) {
|
||||
context.getBeanFactory().registerSingleton(
|
||||
AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
|
||||
multicaster);
|
||||
if (multicaster instanceof BeanFactoryAware) {
|
||||
((BeanFactoryAware) multicaster).setBeanFactory(context.getBeanFactory());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply any relevant post processing the {@link ApplicationContext}. Subclasses can
|
||||
* apply additional processing as required.
|
||||
* @param context the application context
|
||||
*/
|
||||
protected void postProcessApplicationContext(ConfigurableApplicationContext context) {
|
||||
if (this.webEnvironment) {
|
||||
if (context instanceof ConfigurableWebApplicationContext) {
|
||||
ConfigurableWebApplicationContext configurableContext = (ConfigurableWebApplicationContext) context;
|
||||
if (this.beanNameGenerator != null) {
|
||||
configurableContext.getBeanFactory().registerSingleton(
|
||||
AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,
|
||||
this.beanNameGenerator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.resourceLoader != null) {
|
||||
if (context instanceof GenericApplicationContext) {
|
||||
((GenericApplicationContext) context)
|
||||
.setResourceLoader(this.resourceLoader);
|
||||
}
|
||||
if (context instanceof DefaultResourceLoader) {
|
||||
((DefaultResourceLoader) context).setClassLoader(this.resourceLoader
|
||||
.getClassLoader());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply any {@link ApplicationContextInitializer}s to the context before it is
|
||||
* refreshed.
|
||||
|
|
@ -503,59 +529,6 @@ public class SpringApplication {
|
|||
return LogFactory.getLog(this.mainApplicationClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strategy method used to create the {@link ApplicationContext}. By default this
|
||||
* method will respect any explicitly set application context or application context
|
||||
* class before falling back to a suitable default.
|
||||
* @return the application context (not yet refreshed)
|
||||
* @see #setApplicationContextClass(Class)
|
||||
*/
|
||||
protected ConfigurableApplicationContext createApplicationContext() {
|
||||
Class<?> contextClass = this.applicationContextClass;
|
||||
if (contextClass == null) {
|
||||
try {
|
||||
contextClass = Class
|
||||
.forName(this.webEnvironment ? DEFAULT_WEB_CONTEXT_CLASS
|
||||
: DEFAULT_CONTEXT_CLASS);
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
throw new IllegalStateException(
|
||||
"Unable create a default ApplicationContext, "
|
||||
+ "please specify an ApplicationContextClass", ex);
|
||||
}
|
||||
}
|
||||
return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply any relevant post processing the {@link ApplicationContext}. Subclasses can
|
||||
* apply additional processing as required.
|
||||
* @param context the application context
|
||||
*/
|
||||
protected void postProcessApplicationContext(ConfigurableApplicationContext context) {
|
||||
if (this.webEnvironment) {
|
||||
if (context instanceof ConfigurableWebApplicationContext) {
|
||||
ConfigurableWebApplicationContext configurableContext = (ConfigurableWebApplicationContext) context;
|
||||
if (this.beanNameGenerator != null) {
|
||||
configurableContext.getBeanFactory().registerSingleton(
|
||||
AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,
|
||||
this.beanNameGenerator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.resourceLoader != null) {
|
||||
if (context instanceof GenericApplicationContext) {
|
||||
((GenericApplicationContext) context)
|
||||
.setResourceLoader(this.resourceLoader);
|
||||
}
|
||||
if (context instanceof DefaultResourceLoader) {
|
||||
((DefaultResourceLoader) context).setClassLoader(this.resourceLoader
|
||||
.getClassLoader());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load beans into the application context.
|
||||
* @param context the context to load beans into
|
||||
|
|
@ -651,6 +624,33 @@ public class SpringApplication {
|
|||
((AbstractApplicationContext) applicationContext).refresh();
|
||||
}
|
||||
|
||||
private void afterRefresh(ConfigurableApplicationContext context, String[] args) {
|
||||
runCommandLineRunners(context, args);
|
||||
}
|
||||
|
||||
protected void handleFailure(ConfigurableApplicationContext context,
|
||||
ApplicationEventMulticaster multicaster, Throwable exception, String... args) {
|
||||
try {
|
||||
multicaster.multicastEvent(new ApplicationFailedEvent(this, args, context,
|
||||
exception));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// We don't want to fail here and mask the original exception
|
||||
if (this.log.isDebugEnabled()) {
|
||||
this.log.error("Error handling failed", ex);
|
||||
}
|
||||
else {
|
||||
this.log.warn("Error handling failed (" + ex.getMessage() == null ? "no error message"
|
||||
: ex.getMessage() + ")");
|
||||
}
|
||||
}
|
||||
finally {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a specific main application class that will be used as a log source and to
|
||||
* obtain version information. By default the main application class will be deduced.
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ import static org.hamcrest.Matchers.equalTo;
|
|||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
|
@ -130,7 +131,7 @@ public class SpringApplicationTests {
|
|||
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
||||
application.setWebEnvironment(false);
|
||||
this.context = application.run("--spring.application.name=foo");
|
||||
assertEquals("foo", this.context.getId());
|
||||
assertThat(this.context.getId(), startsWith("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue