zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
  • 相关阅读:
    SE知识整理——泛型
    IDEA 运行 SpringMVC 项目分发控制器出现404解决方案。
    快速幂/欧拉降幂
    Leetcode(双指针专题)
    剑指offer
    ns3参考
    网络知识1:最后一公里/WiMax / 4G
    备份2
    shell入门
    ns3_gdb:协议里的函数是怎么被调用的
  • 原文地址:https://www.cnblogs.com/shihaiming/p/9560058.html
Copyright © 2011-2022 走看看