資料新增非同步解析重新整理大資料量redis (——)(五)redisutils
首先要在配置檔案注入這個bean
這也是我非同步重新整理獲取bean用的redisutils工具類:
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
*
* @Description: Spring工具類
* @date 2017年11月19日
* @version V1.0
*
*/
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext context;
public void setApplicationContext(ApplicationContext acx) {
context = acx;
}
public static ApplicationContext getApplicationContext() {
return context;
}
/**
* 獲取Session
* @return
*/
public static HttpSession getSession() {
return getRequest().getSession();
}
/***
* 根據一個bean的id獲取配置檔案中相應的bean
* @param name
* @return
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException
{
return context.getBean(name);
}
public static Object getBean(Class clazz){
return context.getBean(clazz);
}
/**
* 獲取request
* @return
*/
public static HttpServletRequest getRequest() {
ServletRequestAttributes attrs =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return attrs.getRequest();
}
public static HttpServletResponse getResponse() {
return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
}
public static ServletContext getServletContext(){
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
return webApplicationContext.getServletContext();
}