当前位置:   article > 正文

常见Spring相关工具报错-源码分析

常见Spring相关工具报错-源码分析

1. Resouce Bundle 国际化 yml 配置不生效

1️⃣ 配置yml

在这里插入图片描述

2️⃣ 报错信息

  • 2024-04-15 15:13:57.828 [http-nio-8090-exec-1] WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - [doResolveHandlerMethodException,434] - Failure in @ExceptionHandler com.example.config.exception.GlobalExceptionHandle#handleI18nException(I18nException, HttpServletRequest) org.springframework.context.NoSuchMessageException: No message found under code 'global.not.null' for locale 'zh_tw'.

3️⃣ 源码分析 MessageSourceAutoConfiguration 配置类

在 getResources 中会获取 getResources(“classpath*:” + i18n/messages + “.properties”),但是缺少messages.properties属性文件

		private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context, String basename) {
			ConditionMessage.Builder message = ConditionMessage.forCondition("ResourceBundle");
			for (String name : StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename))) {
				for (Resource resource : getResources(context.getClassLoader(), name)) {
					if (resource.exists()) {
						return ConditionOutcome.match(message.found("bundle").items(resource));
					}
				}
			}
			return ConditionOutcome.noMatch(message.didNotFind("bundle with basename " + basename).atAll());
		}

		private Resource[] getResources(ClassLoader classLoader, String name) {
			String target = name.replace('.', '/');
			try {
				return new PathMatchingResourcePatternResolver(classLoader)
						.getResources("classpath*:" + target + ".properties");
			}
			catch (Exception ex) {
				return NO_RESOURCES;
			}
		}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

4️⃣ 解决:补充 messages.properties 文件
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/433837
推荐阅读
相关标签
  

闽ICP备14008679号