springboot实现ApplicationContextAware接口、使用ThreadLocal获得当前上下文请求的HttpServletRequest

前端请求的参数可以用requestparam、requestbody获得,也可以用HttpServletRequest来获得参数,这里讲用HttpServletRequest来获取前端参数的办法:

举例:

  @GetMapping("/index.do")
    @ResponseBody
    public String index() {
        Map map = BasicUtil.assemblyRequestMap();
        ...
        }

BasicUtil.assemblyRequestMap():


	/**
	 * 将请求的request的参数重新组装。主要是将空值的替换成null,因为requestMap空值是"",这样处理有利于外部判断,
	 * 同时将获取到的值映射到页面上
	 *
	 *            HttpServletRequest对象
	 * @return 返回处理过后的数据
	 */
	public static Map<String, Object> assemblyRequestMap() {
		HttpServletRequest request = SpringUtil.getRequest();
		Map<String, Object> params = new HashMap<String, Object>();
		Map<String, String[]> map = request.getParameterMap();
		Iterator<String> key = map.keySet().iterator();
		while (key.hasNext()) {
			String k = (String) key.next().replace(".", "_");
			String[] value = null;
			if(!StringUtil.isBlank(map.get(k))) {
				try {
					 value = map.get(k);
				} catch (java.lang.ClassCastException e) {
					 value = new String[]{map.get(k)+""};
				}
			}


			if (value == null) {
				params.put(k, null);
				request.setAttribute(k, null);
			} else if (value.length == 1) {
				String temp = null;
				if (!StringUtils.isEmpty(value[0])) {
					temp = value[0];
				}
				params.put(k, temp);
				request.setAttribute(k, temp);
			} else if (value.length == 0) {
				params.put(k, null);
				request.setAttribute(k, null);
			} else if (value.length > 1) {
				params.put(k, value);
				request.setAttribute(k, value);
			}
		}
		return params;
	}

SpringUtil.getRequest()在SpringUtil 类中:
关于实现ApplicationContextAware
一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。

@Component
public class SpringUtil implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

	private static ThreadLocal<HttpServletRequest> requestThreadLocal=new ThreadLocal<>();
	/**
	 * 获取当前请求对象
	 *
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		try {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			return request;
		} catch (Exception e) {
			return requestThreadLocal.get();
		}
	}
	/**
	 * 设置当前请求对象
	 *
	 * @return
	 */
	public static void setRequest(HttpServletRequest request) {
		requestThreadLocal.set(request);
	}

	/**
	 * 通过spring的webapplicationcontext上下文对象读取bean对象
	 *
	 * @param sc
	 *            上下文servletConext对象
	 * @param beanName
	 *            要读取的bean的名称
	 * @return 返回获取到的对象。获取不到返回null
	 */
	@Deprecated
	public static Object getBean(ServletContext sc, String beanName) {
		return getApplicationContext().getBean(beanName);
	}

	/**
	 * 通过spring的webapplicationcontext上下文对象读取bean对象
	 *
	 * @param beanName
	 *            要读取的bean的名称
	 * @return 返回获取到的对象。获取不到返回null
	 */
	public static Object getBean(String beanName) {
		return getApplicationContext().getBean(beanName);
	}

	/**
	 * 通过spring的webapplicationcontext上下文对象读取bean对象
	 *
	 * @param cls
	 *            要读取的类名称
	 * @return 返回获取到的对象。获取不到返回null
	 */
	public static <T> T getBean(Class<T> cls) {
		return getApplicationContext().getBean(cls);
	}



	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if (SpringUtil.applicationContext == null) {
			SpringUtil.applicationContext = applicationContext;
		}
	}
	//获取applicationContext
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}
}

这里使用了ThreadLocal,就可以起到线程隔离的作用,不同的客户端的请求互不影响。

你可能感兴趣的:(项目,spring,boot,前端,spring)