Improve startup times

Improve startup times (although not by much) by adding more background
pre-initializers and lazily evaluating the whitelabel SpEL view.

See gh-6177
This commit is contained in:
Phillip Webb 2016-06-22 20:37:59 -07:00
parent 0107d1134a
commit 5182bc4f2b
2 changed files with 41 additions and 5 deletions

View File

@ -24,6 +24,8 @@ import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEven
import org.springframework.boot.logging.LoggingApplicationListener;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
/**
@ -48,6 +50,8 @@ public class BackgroundPreinitializer
runSafely(new MessageConverterInitializer());
runSafely(new MBeanFactoryInitializer());
runSafely(new ValidationInitializer());
runSafely(new JacksonInitializer());
runSafely(new ConverstionServiceInitializer());
}
public void runSafely(Runnable runnable) {
@ -105,4 +109,28 @@ public class BackgroundPreinitializer
}
/**
* Early initializer for Jackson.
*/
private static class JacksonInitializer implements Runnable {
@Override
public void run() {
Jackson2ObjectMapperBuilder.json().build();
}
}
/**
* Early initializer for Spring's ConverstionService.
*/
private static class ConverstionServiceInitializer implements Runnable {
@Override
public void run() {
new DefaultFormattingConversionService();
}
}
}

View File

@ -189,14 +189,11 @@ public class ErrorMvcAutoConfiguration {
private final String template;
private final Map<String, Expression> expressions;
private volatile Map<String, Expression> expressions;
SpelView(String template) {
this.helper = new NonRecursivePropertyPlaceholderHelper("${", "}");
this.template = template;
ExpressionCollector expressionCollector = new ExpressionCollector();
this.helper.replacePlaceholders(this.template, expressionCollector);
this.expressions = expressionCollector.getExpressions();
}
@Override
@ -212,11 +209,22 @@ public class ErrorMvcAutoConfiguration {
}
Map<String, Object> map = new HashMap<String, Object>(model);
map.put("path", request.getContextPath());
PlaceholderResolver resolver = new ExpressionResolver(this.expressions, map);
PlaceholderResolver resolver = new ExpressionResolver(getExpressions(), map);
String result = this.helper.replacePlaceholders(this.template, resolver);
response.getWriter().append(result);
}
private Map<String, Expression> getExpressions() {
if (this.expressions == null) {
synchronized (this) {
ExpressionCollector expressionCollector = new ExpressionCollector();
this.helper.replacePlaceholders(this.template, expressionCollector);
this.expressions = expressionCollector.getExpressions();
}
}
return this.expressions;
}
}
/**