Tianyi's Blog Tianyi's Blog
首页
  • 计算机网络
  • 操作系统
  • 计算机科学
  • Nginx
  • Vue框架
  • 环境配置
  • Java
  • JVM
  • Spring框架
  • Redis
  • MySQL
  • RabbitMQ
  • Kafka
  • Mirror Sites
  • Dev Tools
  • Docker
  • Jenkins
  • Scripts
  • Windows
  • 科学上网
  • 旅行
  • 网站日记
  • 软件
  • 电子产品
  • 杂野
  • 分类
  • 友情链接
GitHub (opens new window)

Tianyi

一直向前,永不停止
首页
  • 计算机网络
  • 操作系统
  • 计算机科学
  • Nginx
  • Vue框架
  • 环境配置
  • Java
  • JVM
  • Spring框架
  • Redis
  • MySQL
  • RabbitMQ
  • Kafka
  • Mirror Sites
  • Dev Tools
  • Docker
  • Jenkins
  • Scripts
  • Windows
  • 科学上网
  • 旅行
  • 网站日记
  • 软件
  • 电子产品
  • 杂野
  • 分类
  • 友情链接
GitHub (opens new window)
  • Java

  • Golang

  • JVM的奇妙世界

  • Spring

  • Spring增强封装

    • 配置类封装-Redis
    • 配置类封装-Async异步注解以及自定义线程池
    • 配置类封装-国际化MessageSource
      • 引言
        • 引包
      • 标准模板
        • 覆盖messageSource同名Bean
      • 自定义WebMvcConfigurer类
        • 自定义MessageSource
        • 简单写法(不支持热加载)
        • 自定义国际化语言拦截器
        • 国际化Util封装
      • 源码
        • 实现
        • 注解
        • MessageSourceProperties
        • @Conditional(ResourceBundleCondition.class)
      • 使用注意点
        • 写法问题
        • 错误的写法
        • 正确写法
        • 验证是不是真的只会读到messages
    • 配置类封装-响应体、全局异常处理
    • 配置类封装-动态切换数据源
    • 配置类封装-RestTemplate
    • 配置类封装-网关限流
  • Redis

  • MySQL

  • RabbitMQ

  • Kafka

  • 分享

  • 后端
  • Spring增强封装
tianyi
2022-09-18
目录

配置类封装-国际化MessageSource

# 引言

国际化的自动配置参照MessageSourceAutoConfiguration,在springboot项目中实现国际化主要分为以下几步:

  1. 添加国际化资源文件 resource(都是放在静态资源的文件夹里面,这没什么好说的,主要遵循格式就好)
  2. 配置messageResource 设置国际化资源文件(覆盖spring自动配置的文件)
  3. 需要去解析请求头中的accept-language 或者 解析url参数中?local=(这里可以修改读取指定字段)
  4. 随意切换本地语言,进行缓存(下面都是一些 api 层面的东西)
  5. 通过messageResource 获取国际化信息
  1. Spring Boot 在类路径根下查找messages资源绑定文件。文件名为:messages.properties
  2. 多语言可以定义多个消息文件,命名为messages_区域代码.properties。如:
    1. messages.properties:默认
    2. messages_zh_CN.properties:中文环境
    3. messages_en_US.properties:英语环境
  3. 在程序中可以自动注入 MessageSource组件,获取国际化的配置项值
    @Autowired  //国际化取消息用的组件
    MessageSource messageSource;
    @GetMapping("/haha")
    public String haha(HttpServletRequest request){
        //利用代码的方式获取国际化配置文件中指定的配置项的值
        return messageSource.getMessage("login", null, request.getLocale());
    }
1
2
3
4
5
6
7

# 引包

<!-- springboot 基础包 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- springboot web包 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10

# 标准模板

# 覆盖messageSource同名Bean

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.SERVLET;

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = SERVLET)
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
 
    /**
     * 语言环境切换拦截器
     *
     * @return
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        // 设置拦截请求url上参数的key,不设置,默认值"locale"
        interceptor.setParamName("lang");
        return interceptor;
    }
 
    /**
     * 自定义语言解析器,覆盖默认的AcceptHeaderLocaleResolver
     * tips:返回类型必须声明为LocaleResolver 接口类型,不然不会替换掉默认的AcceptHeaderLocaleResolver
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINA);  // 设置默认语言环境"中文"
        return localeResolver;
    }
    
    /**
     * 系统国际化文件配置
     * (1)若找不到带对应语言的后缀的properties文件,默认使用xx.properties中的映射关系
     * (2)若映射关系不存在,会抛出一个异常
     *
     * @return MessageSource
     */
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        //指定读取国际化配置文件的basename
        messageSource.setBasenames(ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/error", ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/message");
        //指定编码
        messageSource.setDefaultEncoding("UTF-8");
        //指定缓存时间(Second)
        messageSource.setCacheSeconds(3600);
        return messageSource;
    }
}
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

# 自定义WebMvcConfigurer类

# 自定义MessageSource

@Configuration(proxyBeanMethods = false)注解的作用主要是控制是否使用CGLIB代理来代理@Bean方法。

  • 当 proxyBeanMethods 设置为 true 时,被 @Bean 标识的方法会被 CGLIB 代理,同时也会遵循 Spring 容器的一些生命周期行为,比如 @PostConstruct 和 @Destroy。如果这些 @Bean 方法返回的是单例 Bean,那么在同一个配置类中多次调用这些方法,得到的都是同一个 Bean 实例,因为该 Bean 只会被初始化一次。
  • 当 proxyBeanMethods 设置为 false 时,被 @Bean 标识的方法不会被拦截以进行 CGLIB 代理,也不会遵循 Spring 容器中的生命周期行为。在同一个配置类中调用 @Bean 标识的方法时,仅仅是普通方法的执行,不会从容器中获取对象。即使单独调用 @Bean 标识的方法,也仅仅是普通方法调用,不会走 Bean 的生命周期。
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.SERVLET;

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = SERVLET)
public class WebMvcConfiguration implements WebMvcConfigurer {
    /**
     * 系统国际化文件配置
     * (1)若找不到带对应语言的后缀的properties文件,默认使用xx.properties中的映射关系
     * (2)若映射关系不存在,会抛出一个异常
     *
     * @return MessageSource
     */
    @Bean("messageSource")
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        //指定读取国际化配置文件的basename
        messageSource.setBasenames(ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/error", ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/message");
        //指定编码
        messageSource.setDefaultEncoding("UTF-8");
        //指定缓存时间(Second)
        messageSource.setCacheSeconds(3600);
        return messageSource;
    }
}
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
  • 热加载机制
    • ReloadableResourceBundleMessageSource是支持资源文件热加载的。配置的国际化资源文件的变化将会在下一次请求时被重新加载,可以实现动态更新国际化资源而不需要重启应用程序。
    • ReloadableResourceBundleMessageSource 在重新加载资源文件时,会替换已有的缓存内容。当检测到资源文件已经被修改并重新加载成功后,它会更新内部的缓存,包括之前已经加载的内容。因此,如果在获取 key2 时发现时间有变化并重新加载成功,它会更新缓存中的所有内容,包括 key1,以确保缓存中的内容是最新的。

# 简单写法(不支持热加载)

在配置文件中写,具体原因在源码解析里面会有

spring:
  messages:
    basename: i18n/error,i18n/message
    encoding: UTF-8
    cache-seconds: 3600
1
2
3
4
5

# 自定义国际化语言拦截器

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
 
    /**
     * 语言环境切换拦截器
     *
     * @return
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        // 设置拦截请求url上参数的key,不设置,默认值"locale"
        interceptor.setParamName("lang");
        return interceptor;
    }
 
    /**
     * 自定义语言解析器,覆盖默认的AcceptHeaderLocaleResolver
     * tips:返回类型必须声明为LocaleResolver 接口类型,不然不会替换掉默认的AcceptHeaderLocaleResolver
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        // 设置默认语言环境"中文"
        localeResolver.setDefaultLocale(Locale.CHINA);
        return localeResolver;
    }
}
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
  • localeResolver是bean的名称

# 国际化Util封装

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Locale;

/**
 * 获取国际化消息的翻译工具类。
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class TranslationUtil {

    private final MessageSource messageSource;

    /**
     * 获取国际化消息。
     *
     * @param code  消息码
     * @param param 参数
     * @return 国际化消息
     */
    public String getMessage(String code, Object... param) {
        return getMessage(code, LocaleContextHolder.getLocale(), param);
    }

    /**
     * 获取特定区域设置下的国际化消息。
     *
     * @param code   消息码
     * @param locale 区域
     * @param param  参数
     * @return 国际化消息
     */
    public String getMessage(String code, Locale locale, Object... param) {
        try {
            return messageSource.getMessage(code, param, locale);
        } catch (Exception e) {
            log.error("国际化错误: {}", e.getMessage(), e);
        }
        return "";
    }

    /**
     * 获取 MyExceptionInfoEnum 的国际化消息。
     *
     * @param infoEnum 异常信息
     * @param param    参数
     * @return 国际化异常消息
     */
    public String getMessage(MyExceptionInfoEnum infoEnum, Object... param) {
        return getMessage(infoEnum.getCode(), param);
    }
}
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
49
50
51
52
53
54
55
56
57
  • 尽管 Locale 类型也是 Object 的子类,但是由于方法参数列表中包含了不同的参数类型(Object... 和 Locale)

# 源码

# 实现

@AutoConfiguration
@ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
public class MessageSourceAutoConfiguration {

	private static final Resource[] NO_RESOURCES = {};

	@Bean
	@ConfigurationProperties(prefix = "spring.messages")
	public MessageSourceProperties messageSourceProperties() {
		return new MessageSourceProperties();
	}

	@Bean
	public MessageSource messageSource(MessageSourceProperties properties) {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		if (StringUtils.hasText(properties.getBasename())) {
			messageSource.setBasenames(StringUtils
					.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
		}
		if (properties.getEncoding() != null) {
			messageSource.setDefaultEncoding(properties.getEncoding().name());
		}
		messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
		Duration cacheDuration = properties.getCacheDuration();
		if (cacheDuration != null) {
			messageSource.setCacheMillis(cacheDuration.toMillis());
		}
		messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
		messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
		return messageSource;
	}

	protected static class ResourceBundleCondition extends SpringBootCondition {

		// 省略了

	}

}
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
  • 这是一个自动注入类
  • @ConditionalOnMissingBean:如果容器中不存在这个Bean(组件),则触发指定行为

# 注解

  • @ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)

    • 这里注意要用正确的写法!不能把beanName乱写
  • @Conditional(ResourceBundleCondition.class):找到就能够添加自动注入类,找不到就不行

    • 去 application.yml 里面找配置:

    • messages:
        basename: i18n/message
      
      1
      2

# MessageSourceProperties

  1. basename:指定消息资源文件的基本名称,可以是一个逗号分隔的字符串,用于指定多个资源文件。它遵循 ResourceBundle 的约定,支持相对类路径或绝对路径。如果不包含包限定符(例如"org.mypackage"),则将从类路径根目录解析。
  2. encoding:指定消息资源文件的编码方式,默认为 UTF-8。
  3. cacheDuration:加载的资源包文件缓存持续时间。如果未设置,资源包将永远被缓存。如果未指定持续时间后缀,则默认使用秒。
  4. fallbackToSystemLocale:指定是否在没有找到特定语言环境的文件时,是否回退到系统默认语言环境。如果设置为 false,则只会回退到默认文件(例如,对于基本名称为 "messages",将会回退到 "messages.properties")。
  5. alwaysUseMessageFormat:指定是否总是应用 MessageFormat 规则,即使消息没有参数也会进行解析。
  6. useCodeAsDefaultMessage:指定是否将消息代码用作默认消息,而不是抛出 "NoSuchMessageException"。建议仅在开发阶段使用。

# @Conditional(ResourceBundleCondition.class)

如果最后判断完之后发现没有文件,那么就不满足条件,不会自动加载messageSource类

private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();

    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // 1、有配置:默认检查一下配置文件里面有没有指定路径(配置优先)
        // 2、没有配置:默认baseName为messages
        String basename = context.getEnvironment().getProperty("spring.messages.basename", "messages");
        ConditionOutcome outcome = cache.get(basename);
        if (outcome == null) {
            outcome = getMatchOutcomeForBasename(context, basename);
            cache.put(basename, outcome);
        }
        return outcome;
    }

    private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context, String basename) {
        ConditionMessage.Builder message = ConditionMessage.forCondition("ResourceBundle");
        // 获取到多个以逗号分隔的messageSource
        for (String name : StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename))) {
            // 获取资源:messageSource下的各个语言的文件资源
            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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# 使用注意点

# 写法问题

# 错误的写法

BeanName 名字写错了是找不到的!!!!

image-20240412011359833

这种写法就很搞了:

    @Bean("reloadableResourceBundleMessageSource")
    public MessageSource initReloadableResourceBundleMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        //指定读取国际化配置文件的basename
        messageSource.setBasenames(ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/error", ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/message", ResourceUtils.CLASSPATH_URL_PREFIX + "i18n/warning");
        //指定编码
        messageSource.setDefaultEncoding("UTF-8");
        //指定缓存时间(Second)
        messageSource.setCacheSeconds(3600);
        return messageSource;
    }
1
2
3
4
5
6
7
8
9
10
11

需要指定特定的message,不然没法自动注入指定的MessageSource!

@Autowired
@Qualifier("reloadableResourceBundleMessageSource")
private MessageSource messageSource;
1
2
3

# 正确写法

image-20240411215505671

image-20240412011226379

# 验证是不是真的只会读到messages

1、发现没有messages,不会自动注入

image-20240411221628489

2、注意是messages,不是其他的bundle

image-20240411221817156

3、正确读取到静态资源

image-20240411222018734

完善页面 (opens new window)
配置类封装-Async异步注解以及自定义线程池
配置类封装-响应体、全局异常处理

← 配置类封装-Async异步注解以及自定义线程池 配置类封装-响应体、全局异常处理→

最近更新
01
JDK
02-23
02
BadTasteCode && 优化
09-11
03
Gradle 实践操作指南及最佳实践
09-11
更多文章>
Theme by Vdoing | Copyright © 2021-2025 Tandy | 粤ICP备2023113440号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式