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的更多相关文章

  1. spring 普通类注入为null,通过自定义SpringUtils解决

    package com.jathams.spring; import org.springframework.beans.BeansException; import org.springframew ...

  2. SpringUtils写法

    @Componentpublic class SpringUtils implements ApplicationContextAware { @Override public void setApp ...

  3. Spring在web应用中获得Bean的方法

    一:使用ApplicationContext获得Bean 首先新建一个类,该类必须实现ApplicationContextAware接口,改接口有一个方法,public void setApplica ...

  4. SpringMVC+Freemarker+JSTL支持

    前提: 网页编程中,我的思路是,通用的模块不仅仅只有后台代码,前端页面也可以独立为模块. 这个和asp.net中的UserController很像 比如有个人员基本信息的展示界面,需要在多个界面中嵌入 ...

  5. Spring 4.0.2 学习笔记(1) - 最基本的注入

    1. 添加maven支持 <dependency> <groupId>org.springframework</groupId> <artifactId> ...

  6. Servlet 实现上传文件以及同时,写入xml格式文件和上传

    package com.isoftstone.eply.servlet; import java.io.BufferedReader; import java.io.BufferedWriter; i ...

  7. DaoFactory.java

    package com; import com.isoftstone.fwk.dao.ActionDao; import com.isoftstone.fwk.dao.Dao; import com. ...

  8. 操作失败,没有该服务权限![ 机构号:99 ,用户ID:50000009 ,服务号:0101030112 ]

    操作失败,没有该服务权限![ 机构号:99 ,用户ID:50000009 ,服务号:0101030112 ] 此时我们可以把代码 SERVICE_NO 改成 10个0 即 0000000000 ,就可 ...

  9. VoHelper

    VoHelper package com.isoftstone.pcis.policy.core.helper; import com.isoftstone.fwk.dao.CommonDao; im ...

随机推荐

  1. JavaScript中对象与函数的某些事[JavaScript语言精粹-N1]

    今天在读<JavaScript语言精粹>的时候,关于函数的一个部分,始终觉得有点难以理解,代码如下: 1: var obj = (function(){ 2: var value = 0; ...

  2. oracle行转列

    wm_concat()函数 --创建表 create table test(id number,name varchar2(20)); --插入数据 insert into test values(1 ...

  3. hadoop-2.7.2-HA安装笔记

    配置方案如图   NN DN ZK ZKFC JN RM NM(任务管理器) HMaster Region Server Node1 1 1 1 1 1 Node2 1 1 1 1 1 1 1 Nod ...

  4. 《深入分析JavaWeb技术内幕》学习笔记

    第一章 深入Web请求过程 1.1 B/S网站架构概述 HTTP协议采用无状态的短连接的通信方式.通常一次请求就完成一次数据交互,通常也对应一个业务逻辑. 当在浏览器里输入一个URL,首先会请求DNS ...

  5. ngResource和REST介绍

    ngResource和REST介绍 一.RESTful介绍 RESTful维基百科 REST(表征性状态传输,Representational State Transfer)是Roy Fielding ...

  6. 005.LVM删除

    一 删除LVM步骤 卸载挂载点 移除LV 移除VG 移除PV 删除配置文件 二 卸载挂载点 [root@kauai ~]# umount /dev/vg01/lv01 #先卸载挂载点 三 移除LV [ ...

  7. Web大前端面试题-Day4

    1. 如何实现瀑布流? 瀑布流布局的原理:1) 瀑布流布局要求要进行布置的元素等宽,   然后计算元素的宽度,   与浏览器宽度之比,得到需要布置的列数;2) 创建一个数组,长度为列数,   里面的值 ...

  8. POJ3070 Fibonacci[矩阵乘法]【学习笔记】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  9. 关于Android4.X的Alertdialog对话框

    最近在做Android4.0的开发,发现AlertDialog相比较以前有了较大变化,就是在触摸对话框边缘外部,对话框消失 于是研究其父类发现,可以设置这么一条属性,当然必须先AlertDialog. ...

  10. KVM源代码解读:linux-3.17.4\include\linux\kvm_host.h

    #ifndef __KVM_HOST_H #define __KVM_HOST_H /* * This work is licensed under the terms of the GNU GPL, ...