From 8ff5ce3528bd22ff74fb61f0b3e841bce9bf39f3 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 3 Feb 2014 15:12:31 -0800 Subject: [PATCH] Polish --- .../boot/SpringApplication.java | 182 +++++++++--------- .../boot/SpringApplicationTests.java | 3 +- 2 files changed, 93 insertions(+), 92 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 965b5ed7487..5eafe9d7448 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -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. diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 68c0abeb5b3..1c82191a8d0 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -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