SpringUtils
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.LocaleResolver; import javax.servlet.http.HttpServletRequest;
import java.util.Locale; @Component
public class SpringUtils implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} public static <T> T getBean(Class<T> tClass){
return applicationContext.getBean(tClass);
} public static <T> T getBean(String name, Class<T> type) {
return applicationContext.getBean(name, type);
} public static HttpServletRequest getCurrentReq() {
ServletRequestAttributes requestAttrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (requestAttrs == null) {
return null;
}
return requestAttrs.getRequest();
} public static String getMessage(String code, Object... args) {
LocaleResolver localeResolver = getBean(LocaleResolver.class);
Locale locale = localeResolver.resolveLocale(getCurrentReq());
return applicationContext.getMessage(code, args, locale);
} }
SpringUtils的更多相关文章
- spring 普通类注入为null,通过自定义SpringUtils解决
package com.jathams.spring; import org.springframework.beans.BeansException; import org.springframew ...
- SpringUtils写法
@Componentpublic class SpringUtils implements ApplicationContextAware { @Override public void setApp ...
- Spring在web应用中获得Bean的方法
一:使用ApplicationContext获得Bean 首先新建一个类,该类必须实现ApplicationContextAware接口,改接口有一个方法,public void setApplica ...
- SpringMVC+Freemarker+JSTL支持
前提: 网页编程中,我的思路是,通用的模块不仅仅只有后台代码,前端页面也可以独立为模块. 这个和asp.net中的UserController很像 比如有个人员基本信息的展示界面,需要在多个界面中嵌入 ...
- Spring 4.0.2 学习笔记(1) - 最基本的注入
1. 添加maven支持 <dependency> <groupId>org.springframework</groupId> <artifactId> ...
- Servlet 实现上传文件以及同时,写入xml格式文件和上传
package com.isoftstone.eply.servlet; import java.io.BufferedReader; import java.io.BufferedWriter; i ...
- DaoFactory.java
package com; import com.isoftstone.fwk.dao.ActionDao; import com.isoftstone.fwk.dao.Dao; import com. ...
- 操作失败,没有该服务权限![ 机构号:99 ,用户ID:50000009 ,服务号:0101030112 ]
操作失败,没有该服务权限![ 机构号:99 ,用户ID:50000009 ,服务号:0101030112 ] 此时我们可以把代码 SERVICE_NO 改成 10个0 即 0000000000 ,就可 ...
- VoHelper
VoHelper package com.isoftstone.pcis.policy.core.helper; import com.isoftstone.fwk.dao.CommonDao; im ...
随机推荐
- Go语言之Windows 10开发工具LiteIDE初步使用
Intel Core i5-8250U,Windows 10家庭中文版,go version go1.11 windows/amd64,LiteIDE X34.1 在RUNOOB.COM的Go语言教程 ...
- Tomcat不同版本所对应的Servlet/JSP规范
上午在别人机器上做演示,写好 Servlet居然访问不了.后来回想叻下,觉得应该是Tomcat版本不一致的问题,我用的是Tomcat 7.0建的Project,他们大多数都是用的6.0的版本.回来又仔 ...
- Algorithm类介绍(core)
参考:http://blog.csdn.net/yang_xian521/article/details/7533922
- linux下安装redis及其中遇到的问题的解决方法
1.将下载好的压缩包放到/usr/local目录下# tar xzf redis-3.0.2.tar.gz # cd redis-3.0.2 # make 提示错误 make: cc: Command ...
- LeetCode(22):括号生成
Medium! 题目描述: 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", ...
- C++字符串完全指引
引言 毫无疑问,我们都看到过像 TCHAR, std::string, BSTR 等各种各样的字符串类型,还有那些以 _tcs 开头的奇怪的宏.你也许正在盯着显示器发愁.本指引将总结引进各种字符类型的 ...
- 【Java】 大话数据结构(1) 线性表之顺序存储结构
本文根据<大话数据结构>一书,实现了Java版的顺序存储结构. 顺序存储结构指的是用一段地址连续的存储单元一次存储线性表的数据元素,一般用一维数组来实现. 书中的线性表抽象数据类型定义如 ...
- 【Java】 大话数据结构(11) 查找算法(2)(二叉排序树/二叉搜索树)
本文根据<大话数据结构>一书,实现了Java版的二叉排序树/二叉搜索树. 二叉排序树介绍 在上篇博客中,顺序表的插入和删除效率还可以,但查找效率很低:而有序线性表中,可以使用折半.插值.斐 ...
- Java学习之模拟纸牌游戏,List的ArrayList,Map的HashMap,重写Collections类的sort方法对指定类进行通过特定属性排序,输入异常处理等的学习
首先放上测试效果图 设计框架 具体的代码实现 创建玩家类 public class Player implements Comparable<Player>{ int id; String ...
- 报错:-bash: locate: command not found
-bash: locate: command not found 查看某些文件在哪些地方,需要用到 locate 命令 但是在安装 yum install locate 会报以下错误: -bash: ...