Spring-MessageSource
Spring MessageSource
- Author: HuiFer
- 源码阅读仓库: SourceHot-Spring
初始化入口
org.springframework.context.support.AbstractApplicationContext.refresh
方法有initMessageSource()
方法进行了MessageSource
初始化
|
|
读取 xml 配置文件
getMessage
org.springframework.context.support.AbstractApplicationContext#getMessage(java.lang.String, java.lang.Object[], java.util.Locale)
|
|
-
org.springframework.context.support.AbstractMessageSource#getMessage(java.lang.String, java.lang.Object[], java.util.Locale)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
@Override public final String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException { // 获取对应的信息 String msg = getMessageInternal(code, args, locale); if (msg != null) { return msg; } // 默认信息 null String fallback = getDefaultMessage(code); if (fallback != null) { return fallback; } throw new NoSuchMessageException(code, locale); }
-
两个方法
-
org.springframework.context.support.AbstractMessageSource#getDefaultMessage(java.lang.String)
1 2 3 4 5 6 7 8
@Nullable protected String getDefaultMessage(String code) { // 判断是否使用默认值 if (isUseCodeAsDefaultMessage()) { return code; } return null; }
- 返回 code 本身或者
null
- 返回 code 本身或者
-
org.springframework.context.support.AbstractMessageSource#getMessageInternal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
@Nullable protected String getMessageInternal(@Nullable String code, @Nullable Object[] args, @Nullable Locale locale) { if (code == null) { return null; } if (locale == null) { // 获取语言默认值 locale = Locale.getDefault(); } Object[] argsToUse = args; if (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args)) { // Optimized resolution: no arguments to apply, // therefore no MessageFormat needs to be involved. // Note that the default implementation still uses MessageFormat; // this can be overridden in specific subclasses. String message = resolveCodeWithoutArguments(code, locale); if (message != null) { return message; } } else { // Resolve arguments eagerly, for the case where the message // is defined in a parent MessageSource but resolvable arguments // are defined in the child MessageSource. argsToUse = resolveArguments(args, locale); MessageFormat messageFormat = resolveCode(code, locale); if (messageFormat != null) { synchronized (messageFormat) { return messageFormat.format(argsToUse); } } } // Check locale-independent common messages for the given message code. Properties commonMessages = getCommonMessages(); if (commonMessages != null) { String commonMessage = commonMessages.getProperty(code); if (commonMessage != null) { return formatMessage(commonMessage, args, locale); } } // Not found -> check parent, if any. return getMessageFromParent(code, argsToUse, locale); }
-
-
-
org.springframework.context.support.ResourceBundleMessageSource#resolveCodeWithoutArguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
@Override protected String resolveCodeWithoutArguments(String code, Locale locale) { Set<String> basenames = getBasenameSet(); for (String basename : basenames) { // 加载 basename ResourceBundle bundle = getResourceBundle(basename, locale); if (bundle != null) { // 从basename对应的文件中获取对应的值 String result = getStringOrNull(bundle, code); if (result != null) { return result; } } } return null; }
-
加载后截图
获取方法
String result = getStringOrNull(bundle, code);
就是 map 获取
-
没有配置文件的情况
- 原文作者:知识铺
- 原文链接:https://geek.zshipu.com/post/code/docs/Spring/clazz/Spring-MessageSource/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。
- 免责声明:本页面内容均来源于站内编辑发布,部分信息来源互联网,并不意味着本站赞同其观点或者证实其内容的真实性,如涉及版权等问题,请立即联系客服进行更改或删除,保证您的合法权益。转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。也可以邮件至 sblig@126.com