Skip to content

Commit e520c47

Browse files
committed
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
1 parent cf93f84 commit e520c47

File tree

1 file changed

+3
-70
lines changed

1 file changed

+3
-70
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfiguration.java

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure;
1818

19-
import java.io.IOException;
2019
import java.nio.charset.Charset;
21-
import java.util.Iterator;
22-
import java.util.Set;
2320

2421
import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration.ResourceBundleCondition;
2522
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
@@ -164,8 +161,8 @@ private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context,
164161

165162
private Resource[] getResources(ClassLoader classLoader, String name) {
166163
try {
167-
return new SkipPatternPathMatchingResourcePatternResolver(classLoader)
168-
.getResources("classpath*:" + name + "*.properties");
164+
return new PathMatchingResourcePatternResolver(classLoader)
165+
.getResources("classpath*:" + name + ".properties");
169166
}
170167
catch (Exception ex) {
171168
return NO_RESOURCES;
@@ -174,68 +171,4 @@ private Resource[] getResources(ClassLoader classLoader, String name) {
174171

175172
}
176173

177-
/**
178-
* {@link PathMatchingResourcePatternResolver} that skips well known JARs that don't
179-
* contain messages.properties.
180-
*/
181-
private static class SkipPatternPathMatchingResourcePatternResolver
182-
extends PathMatchingResourcePatternResolver {
183-
184-
private static final ClassLoader ROOT_CLASSLOADER;
185-
186-
static {
187-
ClassLoader classLoader = null;
188-
try {
189-
classLoader = ClassLoader.getSystemClassLoader();
190-
while (classLoader.getParent() != null) {
191-
classLoader = classLoader.getParent();
192-
}
193-
}
194-
catch (Throwable ex) {
195-
// Ignore
196-
}
197-
ROOT_CLASSLOADER = classLoader;
198-
}
199-
200-
private static final String[] SKIPPED = { "aspectjweaver-", "hibernate-core-",
201-
"hsqldb-", "jackson-annotations-", "jackson-core-", "jackson-databind-",
202-
"javassist-", "snakeyaml-", "spring-aop-", "spring-beans-",
203-
"spring-boot-", "spring-boot-actuator-", "spring-boot-autoconfigure-",
204-
"spring-core-", "spring-context-", "spring-data-commons-",
205-
"spring-expression-", "spring-jdbc-", "spring-orm-", "spring-tx-",
206-
"spring-web-", "spring-webmvc-", "tomcat-embed-", "joda-time-",
207-
"hibernate-entitymanager-", "hibernate-validator-", "logback-classic-",
208-
"logback-core-", "thymeleaf-" };
209-
210-
SkipPatternPathMatchingResourcePatternResolver(ClassLoader classLoader) {
211-
super(classLoader);
212-
}
213-
214-
@Override
215-
protected void addAllClassLoaderJarRoots(ClassLoader classLoader,
216-
Set<Resource> result) {
217-
if (classLoader != ROOT_CLASSLOADER) {
218-
super.addAllClassLoaderJarRoots(classLoader, result);
219-
}
220-
}
221-
222-
@Override
223-
protected Set<Resource> doFindAllClassPathResources(String path)
224-
throws IOException {
225-
Set<Resource> resources = super.doFindAllClassPathResources(path);
226-
for (Iterator<Resource> iterator = resources.iterator(); iterator
227-
.hasNext();) {
228-
Resource resource = iterator.next();
229-
for (String skipped : SKIPPED) {
230-
if (resource.getFilename().startsWith(skipped)) {
231-
iterator.remove();
232-
break;
233-
}
234-
}
235-
}
236-
return resources;
237-
}
238-
239-
}
240-
241174
}

0 commit comments

Comments
 (0)