我遇到了一個與加載資源包相關的奇怪問題。經過一些除錯,我能夠找出問題的原因,因為資源包正在按預期順序加載。但是,我查詢是什么原因造成的。我有如下命名的資源包。
core_cs.properties
core_de.properties
core_en_GB.properties
core_en_US.properties
core_en.properties
core_fr.properties
當資源包通過 spring 配置加載時,基本名稱應該是
../core
../core_en
但是,這導致
../core_en
../core
因此,每個語言環境都顯示來自 core_en 資源包的翻譯值。在專案中,我在我的 Web 專案中添加 core.jar 作為依賴項。
當前行為。
found == META-INF/resourcebundles/core_en for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_en_GB.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_cs.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_de.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_es.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_fr.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_hu.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_it.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_nl.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_pl.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_sl.properties
found == META-INF/resourcebundles/core_en for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_en_US.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_en.properties
預期負載應該是
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_cs.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_de.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_en.properties
found == META-INF/resourcebundles/core_en for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_en_GB.properties
found == META-INF/resourcebundles/core_en for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_en_US.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_es.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_fr.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_hu.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_it.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_nl.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_pl.properties
found == META-INF/resourcebundles/core for file jar:file:/D:/Tomcat/WebProject/webapps/demo/WEB-INF/lib/core-SNAPSHOT.jar!/META-INF/resourcebundles/core_sl.properties
我正在使用PathMatchingResourcePatternResolverspringframework 從類路徑配置中決議路徑
<bean id="messageSource" class="com.company.core.spring.support.ClasspathResourceBundleMessageSource">
<property name="basename" value="classpath*:/META-INF/resourcebundles/**/*.properties"/>
</bean>
ClasspathResourceBundleMessageSource是ResourceBundleMessageSource從springframework擴展而來的
public void setBasenames(final String... basenames) {
List<String> basenamesList = new ArrayList<>();
try {
for (String basename : basenames) {
LOGGER.info("==\t Base Name configured {}", basename);
Resource[] resources = (new PathMatchingResourcePatternResolver()).getResources(basename);
for (Resource resource : resources) {
LOGGER.info("==\t Name of resource found {}", resource.getURI());
String filename = resource.getURI().toString();
String newBasename = transformToBasename(filename);
if (newBasename != null && !basenamesList.contains(newBasename)) {
LOGGER.info("Adding resource bundle basename {}", newBasename);
basenamesList.add(newBasename);
}
}
}
// Add the basenames found
LOGGER.info("Resource bundle path:{}", basenamesList);
String[] basenamesArray = basenamesList.toArray(new String[]{});
super.setBasenames(basenamesArray);
} catch (Exception ex) {
LOGGER.error("Error setting base names", ex);
}
}
Another insight is that, if i build archive war locally it works. But, when archive is build by build server (jenkins), then it is not working as expected.
uj5u.com熱心網友回復:
我發現該JarFile.entries()方法 fromjava.util.jar.JarFile以未排序的順序回傳檔案串列。我用一個小程式對其進行了測驗。
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarFileTest {
public static void main(String[] args) {
File correctJarFile = new File("D:\\target\\budget.jar");
File wrongJarFile = new File("D:\\target\\budget2.jar");
try (JarFile jar = new JarFile(correctJarFile )) {
for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
String entryPath = entry.getName();
if (entryPath.endsWith(".properties")) {
System.out.println(entryPath);
}
}
}catch (IOException e) {
}
System.out.println("\n============ 2");
try (JarFile jar = new JarFile(wrongJarFile)) {
for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
String entryPath = entry.getName();
if (entryPath.endsWith(".properties")) {
System.out.println(entryPath);
}
}
}catch (IOException e) {
}
}
}
上面的程式導致下面的輸出。
META-INF/resourcebundles/core_cs.properties
META-INF/resourcebundles/core_de.properties
META-INF/resourcebundles/core_en.properties
META-INF/resourcebundles/core_en_GB.properties
META-INF/resourcebundles/core_en_US.properties
META-INF/resourcebundles/core_fr.properties
============ 2
META-INF/resourcebundles/core_en_GB.properties
META-INF/resourcebundles/core_cs.properties
META-INF/resourcebundles/core_de.properties
META-INF/resourcebundles/core_fr.properties
META-INF/resourcebundles/core_en_US.properties
META-INF/resourcebundles/core_en.properties
這與 Java8 Stream API 相同
try (JarFile jar = new JarFile(jarFile)) {
jar.stream().filter(entry -> entry.getName().endsWith(".properties")).forEach(System.out::println);
} catch (IOException e) {
}
System.out.println("\n============ 0");
try (JarFile jar = new JarFile(jarFile2)) {
jar.stream().filter(entry -> entry.getName().endsWith(".properties")).forEach(System.out::println);
} catch (IOException e) {
}
因此,我已應用自定義排序來修復 ZipFile 中的排序問題,如此 stackoverflow 問題中所述
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/436773.html
