Browse Source

Polish

pull/313/head
Phillip Webb 12 years ago
parent
commit
8ff5ce3528
  1. 154
      spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
  2. 3
      spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

154
spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

@ -362,40 +362,6 @@ public class SpringApplication { @@ -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 { @@ -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,45 +426,6 @@ public class SpringApplication { @@ -464,45 +426,6 @@ public class SpringApplication {
Banner.write(System.out);
}
/**
* Apply any {@link ApplicationContextInitializer}s to the context before it is
* refreshed.
* @param context the configured ApplicationContext (not refreshed yet)
* @see ConfigurableApplicationContext#refresh()
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void applyInitializers(ConfigurableApplicationContext context) {
for (ApplicationContextInitializer initializer : getInitializers()) {
Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(
initializer.getClass(), ApplicationContextInitializer.class);
Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
initializer.initialize(context);
}
}
/**
* Called to log startup information, subclasses may override to add additional
* logging.
* @param isRoot true if this application is the root of a context hierarchy
*/
protected void logStartupInfo(boolean isRoot) {
if (isRoot) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarting(getApplicationLog());
}
}
/**
* Returns the {@link Log} for the application. By default will be deduced.
* @return the application log
*/
protected Log getApplicationLog() {
if (this.mainApplicationClass == null) {
return this.log;
}
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
@ -527,6 +450,17 @@ public class SpringApplication { @@ -527,6 +450,17 @@ public class SpringApplication {
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.
@ -556,6 +490,45 @@ public class SpringApplication { @@ -556,6 +490,45 @@ public class SpringApplication {
}
}
/**
* Apply any {@link ApplicationContextInitializer}s to the context before it is
* refreshed.
* @param context the configured ApplicationContext (not refreshed yet)
* @see ConfigurableApplicationContext#refresh()
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void applyInitializers(ConfigurableApplicationContext context) {
for (ApplicationContextInitializer initializer : getInitializers()) {
Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(
initializer.getClass(), ApplicationContextInitializer.class);
Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
initializer.initialize(context);
}
}
/**
* Called to log startup information, subclasses may override to add additional
* logging.
* @param isRoot true if this application is the root of a context hierarchy
*/
protected void logStartupInfo(boolean isRoot) {
if (isRoot) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarting(getApplicationLog());
}
}
/**
* Returns the {@link Log} for the application. By default will be deduced.
* @return the application log
*/
protected Log getApplicationLog() {
if (this.mainApplicationClass == null) {
return this.log;
}
return LogFactory.getLog(this.mainApplicationClass);
}
/**
* Load beans into the application context.
* @param context the context to load beans into
@ -651,6 +624,33 @@ public class SpringApplication { @@ -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.

3
spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

@ -58,6 +58,7 @@ import static org.hamcrest.Matchers.equalTo; @@ -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 { @@ -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…
Cancel
Save