1.简介
使用EhCache缓存资源,使用ResourcePatternResolver从多个文件中加载资源
2.源文件
1)EhCacheResourceBundles
package com.siyuan.test.spring; import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.Properties; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import net.sf.ehcache.CacheManager; public class EhCacheResourceBundles { private static final Logger LOGGER = LoggerFactory.getLogger(EhCacheResourceBundles.class); private static final String RESOURCE_FILE_SUFFIX = ".properties"; private CacheManager cacheManager; private Map<Locale, EhCacheResourceBundle> resourceBundles = new HashMap<Locale, EhCacheResourceBundle>(); /** * EhCache configuration location * Spring Resource path, a absolute path, * Used to configure the EhCache */ public EhCacheResourceBundles(String configuration, String resourcePatterns) throws IOException { if (StringUtils.isBlank(configuration)) { throw new IllegalArgumentException( "argument[configuration] can not be blank"); } if (StringUtils.isBlank(resourcePatterns)) { throw new IllegalArgumentException( "argument[resourcePatterns] can not be blank"); } initCacheManager(configuration); initResourceBundles(resourcePatterns); } private void initCacheManager(String configuration) throws IOException { ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resourceResolver.getResources(configuration); if (resources.length == 0) { throw new IllegalArgumentException("configuration[" + configuration +"] can not be resolved to a resource"); } InputStream input = null; try { LOGGER.info("init CacheManager by the configuration[{}] ", resources[0]); input = resources[0].getInputStream(); cacheManager = CacheManager.newInstance(input); } finally { if (input != null) { input.close(); } } } private void initResourceBundles(String resourcePatterns) throws IOException { LOGGER.info("load resource from [{}]", resourcePatterns); ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); String[] resourcePatternArray = resourcePatterns.split(","); for (String resourcePattern : resourcePatternArray) { LOGGER.info("load resource from [{}]", resourcePattern); Resource[] resources = resourceResolver.getResources(resourcePattern); for (Resource resource : resources) { loadResource(resource); } } } private void loadResource(Resource resource) throws IOException { String fileName = resource.getFilename(); if (!fileName.endsWith(RESOURCE_FILE_SUFFIX)) { LOGGER.warn("file[{}] does not ended with [{}], will be omitted", resource, RESOURCE_FILE_SUFFIX); return; } InputStream input = null; try { // Locale locale = getLocale(fileName); if (locale == null) { LOGGER.warn("can not resolve locale from file[{}] by name, " + "and the file will be omitted, please make the file name " + "match XXX_xx.properties or XXX_xx_XX.properties pattern", fileName); return; } EhCacheResourceBundle resourceBundle = resourceBundles.get(locale); if (resourceBundle == null) { LOGGER.debug("no EhCacheResourceBundle is corresponding to the " + "locale[{}], will create one", locale); cacheManager.addCache(locale.toString()); resourceBundle = new EhCacheResourceBundle( cacheManager.getCache(locale.toString())); resourceBundles.put(locale, resourceBundle); } input = resource.getInputStream(); Properties properties = new Properties(); properties.load(input); LOGGER.info("load messages from [{}]", resource); for (Object key : properties.keySet()) { resourceBundle.addMessage((String) key, properties.getProperty((String) key)); } } finally { if (input != null) { input.close(); } } } private Locale getLocale(String fileFullName) { Locale locale = null; String fileName = fileFullName.substring(0, fileFullName.lastIndexOf(RESOURCE_FILE_SUFFIX)); String[] fileInfos = fileName.split("_"); if (fileInfos.length == 2) { locale = new Locale(fileInfos[1]); } else if (fileInfos.length == 3) { locale = new Locale(fileInfos[1], fileInfos[2]); } LOGGER.debug("locale[{}] is resolved from the fileName[{}]", locale, fileFullName); return locale; } public String getMessage(String key, Locale locale) { EhCacheResourceBundle resourceBundle = resourceBundles.get(locale); if (resourceBundle == null) { LOGGER.warn("no EhCacheResourceBundle is corresponding to the " + "locale[{}]", locale); return null; } return resourceBundle.getString(key); } public String getMessage(String key, Locale locale, Object...arguments) { String messageFmt = getMessage(key, locale); if (messageFmt == null) { return null; } return new MessageFormat(messageFmt).format(arguments); } }
2)EhCacheResourceBundle
package com.siyuan.test.spring; import java.util.Enumeration; import java.util.ResourceBundle; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; public class EhCacheResourceBundle extends ResourceBundle { private Cache cache; public EhCacheResourceBundle(Cache cache) { this.cache = cache; } public void addMessage(String key, String value) { cache.put(new Element(key, value)); } @Override protected Object handleGetObject(String key) { Element value = cache.get(key); if (key == null) { return null; } return value.getObjectValue(); } @Override public Enumeration<String> getKeys() { throw new UnsupportedOperationException( "EhCacheResourceBundle does not support enumerating its keys"); } }
3)ehcache-i18n.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="autodetect" dynamicConfig="true"> <defaultCache eternal="true" maxEntriesLocalHeap="0" /> </ehcache>
4)资源文件
|-i18n
|-en_US
|-folder
|-test_en_US.properties
test.key1=test.key1.en_US {0}
|-test_en_US.properties
test.key=test.key.en_US
|-zh
|-folder
|-test_zh.properties
test.key1=test.key1.zh {0}
|-test_zh.properties
test.key=test.key.zh
5)EhCacheResourceBundlesTest
import java.io.IOException; import java.util.Locale; import com.siyuan.test.spring.EhCacheResourceBundles; public class EhCacheResourceBundlesTest { public static void main(String[] args) throws IOException { EhCacheResourceBundles resourceBundles = new EhCacheResourceBundles("classpath:ehcache-i18n.xml", "classpath*:i18n/**/*.properties"); System.out.println(resourceBundles.getMessage("test.key", new Locale("zh"))); System.out.println(resourceBundles.getMessage("test.key", new Locale("en", "US"))); System.out.println(resourceBundles.getMessage("test.key1", new Locale("zh"), "test zh")); System.out.println(resourceBundles.getMessage("test.key1", new Locale("en", "US"), "test en_US")); } }
运行结果
test.key.zh test.key.en_US test.key1.zh test zh test.key1.en_US test en_US
3.附件
程序源代码
相关推荐
基于java的贝儿米幼儿教育管理系统答辩PPT.pptx
本压缩包资源说明,你现在往下拉可以看到压缩包内容目录 我是批量上传的基于SpringBoot+Vue的项目,所以描述都一样;有源码有数据库脚本,系统都是测试过可运行的,看文件名即可区分项目~ |Java|SpringBoot|Vue|前后端分离| 开发语言:Java 框架:SpringBoot,Vue JDK版本:JDK1.8 数据库:MySQL 5.7+(推荐5.7,8.0也可以) 数据库工具:Navicat 开发软件: idea/eclipse(推荐idea) Maven包:Maven3.3.9+ 系统环境:Windows/Mac
基于java的消防物资存储系统答辩PPT.pptx
项目经过测试均可完美运行! 环境说明: 开发语言:java jdk:jdk1.8 数据库:mysql 5.7+ 数据库工具:Navicat11+ 管理工具:maven 开发工具:idea/eclipse
项目经过测试均可完美运行! 环境说明: 开发语言:java jdk:jdk1.8 数据库:mysql 5.7+ 数据库工具:Navicat11+ 管理工具:maven 开发工具:idea/eclipse
TA_lib库(whl轮子),直接pip install安装即可,下载即用,非常方便,各个python版本对应的都有。 使用方法: 1、下载下来解压; 2、确保有python环境,命令行进入终端,cd到whl存放的目录,直接输入pip install TA_lib-xxxx.whl就可以安装,等待安装成功,即可使用! 优点:无需C++环境编译,下载即用,方便
使用软件自带的basic脚本编辑制作的脚本 低版本软件无法输出Excel报告,可以通过脚本方式实现这一功能
基于java的就业信息管理系统答辩PPT.pptx
25法理学背诵逻辑.apk.1g
基于java的大学生校园兼职系统答辩PPT.pptx
做到代码,和分析的源数据
本压缩包资源说明,你现在往下拉可以看到压缩包内容目录 我是批量上传的基于SpringBoot+Vue的项目,所以描述都一样;有源码有数据库脚本,系统都是测试过可运行的,看文件名即可区分项目~ |Java|SpringBoot|Vue|前后端分离| 开发语言:Java 框架:SpringBoot,Vue JDK版本:JDK1.8 数据库:MySQL 5.7+(推荐5.7,8.0也可以) 数据库工具:Navicat 开发软件: idea/eclipse(推荐idea) Maven包:Maven3.3.9+ 系统环境:Windows/Mac
项目经过测试均可完美运行! 环境说明: 开发语言:java jdk:jdk1.8 数据库:mysql 5.7+ 数据库工具:Navicat11+ 管理工具:maven 开发工具:idea/eclipse
适用于ensp已经入门人群的学习,有一定难度
基于java的数码论坛系统设计与实现答辩PPT.pptx
tornado-6.4.1-cp38-abi3-macosx_10_9_universal2.whl
基于java的医院信管系统答辩PPT.pptx
项目经过测试均可完美运行! 环境说明: 开发语言:java jdk:jdk1.8 数据库:mysql 5.7+ 数据库工具:Navicat11+ 管理工具:maven 开发工具:idea/eclipse
tornado-4.2.tar.gz