package com.example.demo.utils;
|
|
import org.springframework.beans.BeansException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.stereotype.Component;
|
|
import java.util.*;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* @author Mikey
|
* @Title:
|
* @Description:
|
* @Email:1625017540@qq.com
|
* @date 2018/12/3 21:57
|
* @Version 1.0
|
*/
|
@Component
|
public class SpringUtil implements ApplicationContextAware {
|
private static ApplicationContext applicationContext = null;
|
|
public SpringUtil() {
|
}
|
|
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
|
if (applicationContext == null) {
|
applicationContext = arg0;
|
}
|
|
}
|
|
public static ApplicationContext getApplicationContext() {
|
return applicationContext;
|
}
|
|
public static void setAppCtx(ApplicationContext webAppCtx) {
|
if (webAppCtx != null) {
|
applicationContext = webAppCtx;
|
}
|
}
|
|
/**
|
* 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象
|
*/
|
public static <T> T getBean(Class<T> clazz) {
|
return getApplicationContext().getBean(clazz);
|
}
|
|
public static <T> T getBean(String name, Class<T> clazz) throws ClassNotFoundException {
|
return getApplicationContext().getBean(name, clazz);
|
}
|
|
public static final Object getBean(String beanName) {
|
return getApplicationContext().getBean(beanName);
|
}
|
|
public static final Object getBean(String beanName, String className) throws ClassNotFoundException {
|
Class clz = Class.forName(className);
|
return getApplicationContext().getBean(beanName, clz.getClass());
|
}
|
|
public static boolean containsBean(String name) {
|
return getApplicationContext().containsBean(name);
|
}
|
|
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
|
return getApplicationContext().isSingleton(name);
|
}
|
|
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
|
return getApplicationContext().getType(name);
|
}
|
|
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
|
return getApplicationContext().getAliases(name);
|
}
|
|
|
public static boolean isCarNo(String carNo){
|
if (carNo == null) {
|
return false;
|
}
|
if (carNo.length() >= 7 && carNo.length() <= 8){
|
Pattern p = Pattern.compile("^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([ABCDF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[ABCDF]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})$");
|
Matcher m = p.matcher(carNo);
|
if (!m.matches()){
|
return false;
|
}
|
return true;
|
}else{
|
return false;
|
}
|
}
|
|
//参数类型是Map<String,String> 因为支付只能用string的参数。如果诸君还需要修改的话,那也可以适当的做调整
|
/**
|
*
|
* map转str
|
* @param map
|
* @return
|
*/
|
public static String getMapToString(Map<String, String> map){
|
Set<String> keySet = map.keySet();
|
//将set集合转换为数组
|
String[] keyArray = keySet.toArray(new String[keySet.size()]);
|
//给数组排序(升序)
|
Arrays.sort(keyArray);
|
//因为String拼接效率会很低的,所以转用StringBuilder。博主会在这篇博文发后不久,会更新一篇String与StringBuilder开发时的抉择的博文。
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0; i < keyArray.length; i++) {
|
// 参数值为空,则不参与签名 这个方法trim()是去空格
|
if (map.get(keyArray[i]).trim().length() > 0) {
|
sb.append(keyArray[i]).append("=").append(map.get(keyArray[i]).trim());
|
}
|
if(i != keyArray.length-1){
|
sb.append("&");
|
}
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 2018年10月24日更新
|
* String转map
|
* @param str
|
* @return
|
*/
|
public static Map<String, String> getStringToMap(String str){
|
//感谢bojueyou指出的问题
|
//判断str是否有值
|
if(null == str || "".equals(str)){
|
return null;
|
}
|
//根据&截取
|
String[] strings = str.split("&");
|
//设置HashMap长度
|
int mapLength = strings.length;
|
//判断hashMap的长度是否是2的幂。
|
if((strings.length % 2) != 0){
|
mapLength = mapLength+1;
|
}
|
|
Map<String, String> map = new HashMap<>(mapLength);
|
//循环加入map集合
|
for (int i = 0; i < strings.length; i++) {
|
//截取一组字符串
|
String[] strArray = strings[i].split("=");
|
//strArray[0]为KEY strArray[1]为值
|
map.put(strArray[0],strArray[1]);
|
}
|
return map;
|
}
|
|
|
/**
|
* 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
|
* @param params 需要排序并参与字符拼接的参数组
|
* @return 拼接后字符串
|
* @throws UnsupportedEncodingException
|
*/
|
public static String createLinkString(Map<String, String> params) {
|
|
|
Map<String, String> mapparams =sortMapByKey(params);
|
if (mapparams == null) {
|
return "";
|
}
|
List<String> keys = new ArrayList<String>(mapparams.keySet());
|
Collections.sort(keys);
|
String prestr = "";
|
for (int i = 0; i < keys.size(); i++) {
|
String key = keys.get(i);
|
String value = mapparams.get(key);
|
if (i == keys.size() - 1) {////拼接时,不包括最后一个&字符
|
prestr = prestr + key + "=" + value;
|
} else {
|
prestr = prestr + key + "=" + value + "&";
|
}
|
}
|
return prestr;
|
}
|
|
/**
|
* 使用 Map按key进行排序
|
* @param map
|
* @return
|
*/
|
public static Map<String, String> sortMapByKey(Map<String, String> map) {
|
if (map == null || map.isEmpty()) {
|
return null;
|
}
|
|
Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());
|
|
sortMap.putAll(map);
|
|
return sortMap;
|
}
|
|
|
}
|