Spring加载classpath与classpath*的过程剖析
使用spring-boot 1.5.7
在resource目录下创建i18n文件夹
使用spring的默认配置没有加载到文件
# INTERNATIONALIZATION (MessageSourceAutoConfiguration)
spring.messages.always-use-message-format=false # Set whether to always apply the MessageFormat rules, parsing even messages without arguments.
spring.messages.basename=messages # Comma-separated list of basenames, each following the ResourceBundle convention.
spring.messages.cache-seconds=-1 # Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles are cached forever.
spring.messages.encoding=UTF-8 # Message bundles encoding.
spring.messages.fallback-to-system-locale=true # Set whether to fall back to the system Locale if no files for a specific Locale have been found.
在 MessageSourceAutoConfiguration中看到如下代码:

不是很明白classpath*是什么意思
PathMatchingResourcePatternResolver中的方法实现
@Override
public Resource[] getResources(String locationPattern) throws IOException {
Assert.notNull(locationPattern, "Location pattern must not be null");
if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
// a class path resource (multiple resources for same name possible)
// CLASSPATH_ALL_URL_PERFIX = "classpath*";
//getPathMatcher().isPattern("messages.properties") 返回false
if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
// a class path resource pattern
return findPathMatchingResources(locationPattern);
}
else {
// all class path resources with the given name
return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
}
}
else {
// Generally only look for a pattern after a prefix here,
// and on Tomcat only after the "*/" separator for its "war:" protocol.
int prefixEnd = (locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 :
locationPattern.indexOf(":") + 1);
if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
// a file pattern
return findPathMatchingResources(locationPattern);
}
else {
// a single resource with the given name
return new Resource[] {getResourceLoader().getResource(locationPattern)};
}
}
}
解决方法:
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:i18n/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
ResourceBundleMessageSource限制了我们只能将对应的资源文件放置在类路径下
ReloadableResourceBundleMessageSource允许我们指定非类路径下的文件作为对应的资源文件
Spring加载classpath与classpath*的过程剖析的更多相关文章
- 【转载】Spring加载resource时classpath*:与classpath:的区别
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:kyfxbl 原文地址: spring配置中classpath和cla ...
- Spring加载resource时classpath*:与classpath:的区别
http://blog.csdn.net/kkdelta/article/details/5507799 classpath: 第一个匹配的 classpath*:多个组件中的可匹配的
- 【Spring 源码】Spring 加载资源并装配对象的过程(XmlBeanDefinitionReader)
Spring 加载资源并装配对象过程 在Spring中对XML配置文件的解析从3.1版本开始不再推荐使用XmlBeanFactory而是使用XmlBeanDefinitionReader. Class ...
- spring加载资源文件中classpath*与classpath的区别
在spring和MyBatis继承的时候,配置mapperLocations.一开始配置是这样的. 需要加载路径为com/thomas/base/mapper和com/thomas/bu/mapper ...
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- spring加载jar包中多个配置文件(转)
转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...
- 【Java Web开发学习】Spring加载外部properties配置文件
[Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...
- Spring加载流程源码分析03【refresh】
前面两篇文章分析了super(this)和setConfigLocations(configLocations)的源代码,本文来分析下refresh的源码, Spring加载流程源码分析01[su ...
- Dubbo实践(六)Spring加载Bean流程
根据上一小节对于spring扩展schema的介绍,大概可以猜到dubbo中相关的内容是如何实现的. 再来回顾Dubbo实践(一)中定义的dubbo-provider.xml: <?xml ve ...
- spring加载配置文件
spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...
随机推荐
- Json Web Token(JWT)
Json web token (JWT),是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(Si ...
- CFA一级知识点总结
更多来自: www.vipcoursea.com Ethics 部分 Objective of codes and standard:永远是为了maintain public trust in ...
- ASP.NET MVC 4 - 上传图片到数据库
这里演示如何在MVC WEB应用程序如何上传图片到数据库以及如何在WEB页面上显示图片.数据库表对应整个Model类,不单图片数据一个字段,我们从数据表的定义开始: CREATE TABLE [dbo ...
- [Linux] 修改系统默认编码
locale 命令 locale 命令用以设置程序运行的语言环境. locale 设置语言环境的命名规则为 Language_area.charset,例如 en_US.utf8 表示语言为英语,地区 ...
- Excel 函数集(使用过的)
1. SUBTOTAL函数 筛选结果求和 SUBTOTAL(函数编号, 区域) 函数编号 为 1 到 11(包含隐藏值)或 101 到 111(忽略隐藏值)之间的数字,指定使用何种函数在数据清单 ...
- [原]openstack-kilo--issue(十七) heat创建网络Quota exceeded for resources OverQuotaClient: resources.dmz_protected_network_sub
----- 1 ------- 在使用heat创建网络的时候,报错如下 INFO heat.engine.stack [-] Stack CREATE FAILED (mmsc_network_s ...
- The app references non-public selector in MyApp : id
id<FBGraphUser>friend id<FBGraphUserExtraFields>user 应该使用 [user objectForKey:@"id ...
- 美团店铺评价语言处理以及分类(tfidf,SVM,决策树,随机森林,Knn,ensemble)
第一篇 数据清洗与分析部分 第二篇 可视化部分, 第三篇 朴素贝叶斯文本分类 支持向量机分类 支持向量机 网格搜索 临近法 决策树 随机森林 bagging方法 import pandas as pd ...
- CentOS和Redhat救援模式
当单用户模式无法修复系统时可以使用救援模式 把系统盘插入光驱重启主机 选择救援模式 默认选择 是否设置网络这里选择否 已读写模式挂载 提示系统挂载/mnt/sysimage如果需要恢复运行 chroo ...
- WPF datagrid 获取行或单格为NULL 问题
datagrid 属性 EnableRowVirtualization 设置为 false 解决...不要问我为什么. 害死我了