做本地婚恋网站,广东seo推广费用,网站建设招标说明书,深圳网站建设网络推广公司java后端自学错误总结 MessageSource国际化接口总结 MessageSource国际化接口
今天第一次使用MessageSource接口,比较意外遇到了一些坑 messageSource是spring中的转换消息接口#xff0c;提供了国际化信息的能力。MessageSource用于解析 消息#xff0c;并支持消息的参数化… java后端自学错误总结 MessageSource国际化接口总结 MessageSource国际化接口
今天第一次使用MessageSource接口,比较意外遇到了一些坑 messageSource是spring中的转换消息接口提供了国际化信息的能力。MessageSource用于解析 消息并支持消息的参数化和国际化。 Spring 包含两个内置的MessageSource实 现ResourceBundleMessageSource和ReloadableResourceBundleMessageSource。 我是用的是idea工具进行开发
文件中文乱码情况,需要先设置一下idea的编码格式,一定需要设置,要不然直接再文件里面将乱码改成中文会有问题的,出现的问题现象就是第一张图是我没有设置编码的时候的样子,第二张是我改为中文的样子,我按照第二张图运行了代码导致我获得的值是???,4个文号,所以大大家不要和我一样傻直接改文件,按照第三张图配置一下就改为中文了 2.下面是我的代码展示
2.1书写全局异常处理类
/*** program:* description: 全局异常拦截处理类* author: wsw* create: 2023-11-29 10:21**/
Log4j2
ControllerAdvice//控制器增强
public class ExceptionCatch {/*** 捕获异常*/ResponseBodyExceptionHandler({Exception.class})//异常处理器与上面的注解一起使用,可以拦截指定的异常信息public ResponseResult exception(Exception exception) {//获取exception异常的类型exception.printStackTrace();//记录日志log.error(catch exception:{}, exception.getMessage());//返回通用的异常return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,exception.getMessage());}
}2.2书写spring工具类
Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{/** Spring应用上下文环境 */private static ConfigurableListableBeanFactory beanFactory;private static ApplicationContext applicationContext;Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException{SpringUtils.beanFactory beanFactory;}Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException{SpringUtils.applicationContext applicationContext;}/*** 获取对象** param name* return Object 一个以所给名字注册的bean的实例* throws org.springframework.beans.BeansException**/SuppressWarnings(unchecked)public static T T getBean(String name) throws BeansException{return (T) beanFactory.getBean(name);}/*** 获取类型为requiredType的对象** param clz* return* throws org.springframework.beans.BeansException**/public static T T getBean(ClassT clz) throws BeansException{T result (T) beanFactory.getBean(clz);return result;}/*** 如果BeanFactory包含一个与所给名称匹配的bean定义则返回true** param name* return boolean*/public static boolean containsBean(String name){return beanFactory.containsBean(name);}/*** 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到将会抛出一个异常NoSuchBeanDefinitionException** param name* return boolean* throws org.springframework.beans.factory.NoSuchBeanDefinitionException**/public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException{return beanFactory.isSingleton(name);}/*** param name* return Class 注册对象的类型* throws org.springframework.beans.factory.NoSuchBeanDefinitionException**/public static Class? getType(String name) throws NoSuchBeanDefinitionException{return beanFactory.getType(name);}/*** 如果给定的bean名字在bean定义中有别名则返回这些别名** param name* return* throws org.springframework.beans.factory.NoSuchBeanDefinitionException**/public static String[] getAliases(String name) throws NoSuchBeanDefinitionException{return beanFactory.getAliases(name);}/*** 获取aop代理对象** param invoker* return*/SuppressWarnings(unchecked)public static T T getAopProxy(T invoker){return (T) AopContext.currentProxy();}/*** 获取当前的环境配置无配置返回null** return 当前的环境配置*/public static String[] getActiveProfiles(){return applicationContext.getEnvironment().getActiveProfiles();}/*** 获取当前的环境配置当有多个环境配置时只获取第一个** return 当前的环境配置*/public static String getActiveProfile(){final String[] activeProfiles getActiveProfiles();return !( activeProfilesnull || (activeProfiles.length 0))? activeProfiles[0] : null;}/*** 获取配置文件中的值** param key 配置文件的key* return 当前的配置文件的值**/public static String getRequiredProperty(String key){return applicationContext.getEnvironment().getRequiredProperty(key);}
}2.3书写扫描异常拦截处理类配置类
Configuration
ComponentScan(****.****.****.****)//上面异常处理类的路径
public class ExceptionConfig {
}2.4书写扫描spring工具类扫描
/*** program:* description: 扫描spring工具类包* author: wsw* create: 2023-11-29 13:19**/
Configuration
ComponentScan(****.****.****.****)//上面spring工具类的路径
public class SpringUtilsConfig {
}
2.5自定义异常基础类
public class BaseException extends RuntimeException {private static final long serialVersionUID1L;/*** 所属模块*/private String module;/*** 错误码*/private String code;/*** 错误码对应的参数*/private Object[] args;/*** 错误消息*/private String defaultMessage;public BaseException(String module, String code, Object[] args, String defaultMessage){this.module module;this.code code;this.args args;this.defaultMessage defaultMessage;}public BaseException(String module, String code, Object[] args){this(module, code, args, null);}public BaseException(String module, String defaultMessage){this(module, null, null, defaultMessage);}public BaseException(String code, Object[] args){this(null, code, args, null);}public BaseException(String defaultMessage){this(null, null, null, defaultMessage);}Overridepublic String getMessage(){String message null;if (!StringUtils.isEmpty(code)){message MessageUtils.message(code, args);}if (message null){message defaultMessage;}return message;}public String getModule(){return module;}public String getCode(){return code;}public Object[] getArgs(){return args;}public String getDefaultMessage(){return defaultMessage;}}
2.6自定义异常继承基础类
public class UserException extends BaseException
{private static final long serialVersionUID 1L;public UserException(String code, Object[] args){super(user, code, args, null);}
}2.7获取i18n文件将数据交给pring messageSource /*** 获取i18n资源文件* * author wsw*/
public class MessageUtils
{/*** 根据消息键和参数 获取消息 委托给spring messageSource** param code 消息键* param args 参数* return 获取国际化翻译值*/public static String message(String code, Object... args){MessageSource messageSource SpringUtils.getBean(MessageSource.class);return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());}
}2.8配置i18n国际化文件messages.properties
not.null* 必须填写
user.jcaptcha.error验证码错误
user.jcaptcha.expire验证码已失效
user.not.exists用户不存在/密码错误2.9配置spring资源信息国际化资源文件路径application.yml
spring:# 资源信息messages:# 国际化资源文件路径basename: i18n/messages总结
配置idea编码如果springUtils工具类调用报错空指针异常,请扫描一下springUtils这个包,也就是上面写的springUtilsConfig类,否则 beanFactory会一直是空值