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

本文探讨了Spring应用中ResourceBundle国际化配置不生效的问题,通过源码分析发现是由于缺少messages.properties文件。解决方案是检查并补充该文件,确保资源加载正确。
摘要由CSDN通过智能技术生成

常见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;
			}
		}

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

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值