Refine `messages.properties` detection
Update `ResourceBundleCondition` to only enable the messages source if `messages.properties` (and not `messages*.properties`) exists. This operation is much faster that performing a pattern match since a full jar entry scan is not required. Since adding `messages.properties` is good practice and this change allows us to delete the custom `PathMatchingResourcePatternResolver` it seems like a fine compromise to make. Fixes gh-4930 See gh-4811
This commit is contained in:
parent
cf93f84e87
commit
e520c47c4f
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2015 the original author or authors.
|
* Copyright 2012-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,10 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure;
|
package org.springframework.boot.autoconfigure;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition;
|
import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||||
|
@ -164,8 +161,8 @@ public class MessageSourceAutoConfiguration {
|
||||||
|
|
||||||
private Resource[] getResources(ClassLoader classLoader, String name) {
|
private Resource[] getResources(ClassLoader classLoader, String name) {
|
||||||
try {
|
try {
|
||||||
return new SkipPatternPathMatchingResourcePatternResolver(classLoader)
|
return new PathMatchingResourcePatternResolver(classLoader)
|
||||||
.getResources("classpath*:" + name + "*.properties");
|
.getResources("classpath*:" + name + ".properties");
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
return NO_RESOURCES;
|
return NO_RESOURCES;
|
||||||
|
@ -174,68 +171,4 @@ public class MessageSourceAutoConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link PathMatchingResourcePatternResolver} that skips well known JARs that don't
|
|
||||||
* contain messages.properties.
|
|
||||||
*/
|
|
||||||
private static class SkipPatternPathMatchingResourcePatternResolver
|
|
||||||
extends PathMatchingResourcePatternResolver {
|
|
||||||
|
|
||||||
private static final ClassLoader ROOT_CLASSLOADER;
|
|
||||||
|
|
||||||
static {
|
|
||||||
ClassLoader classLoader = null;
|
|
||||||
try {
|
|
||||||
classLoader = ClassLoader.getSystemClassLoader();
|
|
||||||
while (classLoader.getParent() != null) {
|
|
||||||
classLoader = classLoader.getParent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Throwable ex) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
ROOT_CLASSLOADER = classLoader;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String[] SKIPPED = { "aspectjweaver-", "hibernate-core-",
|
|
||||||
"hsqldb-", "jackson-annotations-", "jackson-core-", "jackson-databind-",
|
|
||||||
"javassist-", "snakeyaml-", "spring-aop-", "spring-beans-",
|
|
||||||
"spring-boot-", "spring-boot-actuator-", "spring-boot-autoconfigure-",
|
|
||||||
"spring-core-", "spring-context-", "spring-data-commons-",
|
|
||||||
"spring-expression-", "spring-jdbc-", "spring-orm-", "spring-tx-",
|
|
||||||
"spring-web-", "spring-webmvc-", "tomcat-embed-", "joda-time-",
|
|
||||||
"hibernate-entitymanager-", "hibernate-validator-", "logback-classic-",
|
|
||||||
"logback-core-", "thymeleaf-" };
|
|
||||||
|
|
||||||
SkipPatternPathMatchingResourcePatternResolver(ClassLoader classLoader) {
|
|
||||||
super(classLoader);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void addAllClassLoaderJarRoots(ClassLoader classLoader,
|
|
||||||
Set<Resource> result) {
|
|
||||||
if (classLoader != ROOT_CLASSLOADER) {
|
|
||||||
super.addAllClassLoaderJarRoots(classLoader, result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Set<Resource> doFindAllClassPathResources(String path)
|
|
||||||
throws IOException {
|
|
||||||
Set<Resource> resources = super.doFindAllClassPathResources(path);
|
|
||||||
for (Iterator<Resource> iterator = resources.iterator(); iterator
|
|
||||||
.hasNext();) {
|
|
||||||
Resource resource = iterator.next();
|
|
||||||
for (String skipped : SKIPPED) {
|
|
||||||
if (resource.getFilename().startsWith(skipped)) {
|
|
||||||
iterator.remove();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resources;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue