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);
        }
    
    }
  • 相关阅读:
    MVC Form
    The way to learn english
    Test FastThree
    C#中Trim()、TrimStart()、TrimEnd()的用法
    c# Dictionary 简介
    visual studio快捷键大全
    ASP.NET MVC 中 ActionResult
    MVC4中使用 Ninject
    MVC Chapter 12 Overview of MVC Projects
    ASP.NET Razor
  • 原文地址:https://www.cnblogs.com/shihaiming/p/9560058.html
Copyright © 2011-2022 走看看