当前位置: 首页 > news >正文

抓好网站建设工作抖音推广佣金平台

抓好网站建设工作,抖音推广佣金平台,哪个大学的网站做的最好看,网站wap版怎么做java后端自学错误总结 MessageSource国际化接口总结 MessageSource国际化接口 今天第一次使用MessageSource接口,比较意外遇到了一些坑 messageSource是spring中的转换消息接口,提供了国际化信息的能力。MessageSource用于解析 消息,并支持消息的参数化…

java后端自学错误总结

  • MessageSource国际化接口
  • 总结

MessageSource国际化接口

今天第一次使用MessageSource接口,比较意外遇到了一些坑

messageSource是spring中的转换消息接口,提供了国际化信息的能力。MessageSource用于解析
消息,并支持消息的参数化和国际化。 Spring 包含两个内置的MessageSource实
现:ResourceBundleMessageSource和ReloadableResourceBundleMessageSource。

我是用的是idea工具进行开发

  1. 文件中文乱码情况,需要先设置一下idea的编码格式,一定需要设置,要不然直接再文件里面将乱码改成中文会有问题的,出现的问题现象就是第一张图是我没有设置编码的时候的样子,第二张是我改为中文的样子,我按照第二张图运行了代码导致我获得的值是???,4个文号,所以大大家不要和我一样傻直接改文件,按照第三张图配置一下就改为中文了

在这里插入图片描述在这里插入图片描述
在这里插入图片描述2.下面是我的代码展示

2.1书写全局异常处理类

/*** @program:* @description: 全局异常拦截处理类* @author: wsw* @create: 2023-11-29 10:21**/
@Log4j2
@ControllerAdvice//控制器增强
public class ExceptionCatch {/*** 捕获异常*/@ResponseBody@ExceptionHandler({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(Class<T> 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 !( activeProfiles==null || (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 serialVersionUID=1L;/*** 所属模块*/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

总结

  1. 配置idea编码
  2. 如果springUtils工具类调用报错空指针异常,请扫描一下springUtils这个包,也就是上面写的springUtilsConfig类,否则 beanFactory会一直是空值
http://www.mmbaike.com/news/91045.html

相关文章:

  • 用wordpress写网页优化公司治理结构
  • 网站后台教程湖南网站建设加盟代理
  • 捷克注册公司网站酒店营销推广方案
  • seo网站优化线上宣传方式有哪些
  • 海口网站建设推广市场推广策略 包括哪些
  • 做购物网站怎拼找商家alexa
  • 寻找哈尔滨网站建设服务之家网站推广公司
  • 二手房公司网站源码网店培训
  • 哪些企业会考虑做网站徐州自动seo
  • wordpress管理员手册成都网站seo报价
  • 如何做网站详细步骤图株洲24小时新闻
  • 怎么做网站省钱百度关键词优化查询
  • 网站申请微信登录知乎关键词排名优化工具
  • 做企业宣传网站公司地推拉新app推广平台有哪些
  • 平台投诉举报热线电话seo诊断分析在线工具
  • 废旧网站那个做的最好爱上链外链购买平台
  • 流量统计网站推广法腾讯营销平台
  • 政府网站建设模板 html上海百度推广优化
  • 厦门公司网站设计网络推广是做什么工作
  • 中山免备案网站建设全球搜索引擎大全
  • 做网站_没内容站长工具ping
  • 别人做的网站如何要回服务器郑州网站推广排名公司
  • seo如何建立优化网站买链接网站
  • 南京网站建设 雷牛奶推广软文文章
  • 做网站有哪些语言网络营销的主要方式和技巧
  • 找代做海报的网站网络营销策划书模板
  • 设计师网站pin网站排名优化培训
  • 中英文网站建设用两个域名软件开发需要多少资金
  • 电子商务网站建设公服务外包平台
  • 如何建设企业网站ppt免费注册