接口的作用
当一个类实现了这个接口(ApplicationContextAware)之后,Aware接口的Bean在被初始之后,可以取得一些相对应的资源,这个类可以直接获取spring 配置文件中 所有引用(注入)到的bean对象。
项目中的用法
1 @Component 2 public class WebUtil implements ApplicationContextAware { 3 4 private static ApplicationContext applicationContext; 5 6 public static final String COMPUTER = "C" ; //"Computer" 7 public static final String MOBILE = "M" ; //"Mobile" 8 public static final String UNKNOWN = "Unknown" ; 9 public static final String USER_AGENT = "User-Agent"; 10 11 /** 12 * 取得实际路径 13 */ 14 public static String getRealPath(String path) { 15 return getHttpServletRequest().getServletContext().getRealPath(path); 16 } 17 18 /** 19 * 从IOC中获得对象: 根据类型 20 */ 21 public static <T> T getBean(Class<T> requiredType){ 22 return applicationContext.getBean(requiredType); 23 } 24 25 /** 26 * 从IOC中获得对象: 根据id 27 */ 28 public static Object getBean(String beanName){ 29 return applicationContext.getBean(beanName); 30 } 31 32 /** 33 * 从IOC中获得多个对象(实现接口或继承的) 34 */ 35 public static <T> Map<String, T> getBeansOfType(Class<T> requiredType){ 36 return applicationContext.getBeansOfType(requiredType); 37 } 38 39 /** 40 * 获得当前请求的Request对象 41 */ 42 public static HttpServletRequest getHttpServletRequest() { 43 return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 44 } 45 46 /** 47 * 取得设备类型 48 */ 49 public static String getDeviceType() { 50 String userAgent = getHttpServletRequest().getHeader(USER_AGENT); 51 return getDeviceType(userAgent); 52 } 53 54 /** 55 * 当前是否是电脑设备 56 */ 57 public static boolean isComputer() { 58 return COMPUTER.equals(getDeviceType()); 59 } 60 /** 61 * 获得设备类型 62 */ 63 public static String getDeviceType(String userAgentHeader) { 64 if (userAgentHeader == null) return COMPUTER; 65 if (userAgentHeader.contains("Andriod") || userAgentHeader.contains("iPhone") || userAgentHeader.contains("iPad")) 66 return MOBILE; 67 return COMPUTER; 68 } 69 70 /** 71 * 取得浏览器 72 */ 73 public static String getBrowser(String userAgentHeader) { 74 UserAgent userAgent = UserAgent.parseUserAgentString(userAgentHeader); 75 String brower = userAgent.getBrowser().getName(); 76 77 //考虑手机端的浏览器访问,此处采用解析为Unknown时空格分隔取第一个 78 if (Browser.UNKNOWN.getName().equals(brower) && userAgentHeader != null) { 79 brower = userAgentHeader.split(" ")[0]; 80 } 81 return brower; 82 } 83 84 /** 85 * 取得操作系统 86 */ 87 public static String getOsName(String userAgentHeader) { 88 UserAgent userAgent = UserAgent.parseUserAgentString(userAgentHeader); 89 String osName = userAgent.getOperatingSystem().getName(); 90 return osName; 91 } 92 93 /** 94 * 根据Request获取IP地址 95 * 96 * 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址 97 * 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个, 98 * 而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址 99 */ 100 public static String getIpAddr(HttpServletRequest request) { 101 // 代理处理 102 String ip = request.getHeader("x-forwarded-for"); 103 if (blankOrUnknown(ip)) ip = request.getHeader("Proxy-Client-IP"); 104 if (blankOrUnknown(ip)) ip = request.getHeader("WL-Proxy-Client-IP"); 105 if (blankOrUnknown(ip)) ip = request.getHeader("HTTP_CLIENT_IP"); 106 if (blankOrUnknown(ip)) ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 107 108 // 多级代理 109 if (StringUtils.isNotBlank(ip) && ip.contains(",")) ip = ip.split(",")[0]; 110 111 // 正常处理 112 if (blankOrUnknown(ip)) ip = request.getRemoteAddr(); 113 114 // 特殊设置 115 if("0:0:0:0:0:0:0:1".equals(ip) || "localhost".equals(ip)) ip = "127.0.0.1"; 116 117 //非空限定 118 if(ip == null) ip = "unknown"; 119 120 return ip; 121 } 122 123 //IP地址为空(白)或为unknown 124 private static boolean blankOrUnknown(String ip) { 125 return StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip); 126 } 127 128 /** 129 * 是否为ajax请求 130 */ 131 public static boolean isAjax(HttpServletRequest request){ 132 //如果是ajax请求响应头会有,x-requested-with 133 if (request.getHeader("x-requested-with") != null 134 && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) { 135 return true; 136 } 137 return false; 138 } 139 140 @Override 141 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 142 WebUtil.applicationContext = applicationContext; 143 } 144 145 public static ApplicationContext getApplicationContext() { 146 return applicationContext; 147 } 148 149 150 }
用法:
LogQuartzService logQuartzService =WebUtil.getBean(LogQuartzService.class);