init
This commit is contained in:
101
src/main/java/com/newyetai/common/util/SpringContextHolder.java
Normal file
101
src/main/java/com/newyetai/common/util/SpringContextHolder.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package com.newyetai.common.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
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.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
/**
|
||||
* spring上下文工具类
|
||||
*
|
||||
* @author sr on 2020-05-20
|
||||
*/
|
||||
@Component
|
||||
public class SpringContextHolder implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
SpringContextHolder.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取bean
|
||||
*
|
||||
* @param <T>
|
||||
* @param beanId
|
||||
* @return
|
||||
* @author sr on 2020-05-20
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getBean(String beanId) {
|
||||
return (T) applicationContext.getBean(beanId);
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return applicationContext.getBean(clazz);
|
||||
}
|
||||
|
||||
public static Optional<ServletRequestAttributes> getRequestAttributes() {
|
||||
return Optional
|
||||
.ofNullable((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的request
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
return getRequestAttributes().map(ServletRequestAttributes :: getRequest).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的response
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
return getRequestAttributes().map(ServletRequestAttributes :: getResponse).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取HttpSession
|
||||
*/
|
||||
public static HttpSession getHttpSession() {
|
||||
return getRequest().getSession();
|
||||
}
|
||||
|
||||
/**
|
||||
* 把值放入session
|
||||
*/
|
||||
public static void setSessionAttribute(String key, Serializable entity) {
|
||||
getHttpSession().setAttribute(key, entity);
|
||||
}
|
||||
|
||||
public static Object getSessionAttribute(String key) {
|
||||
return getHttpSession().getAttribute(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把值放入当前request
|
||||
*/
|
||||
public static void setRequestAttribute(String key, Object entity) {
|
||||
getRequest().setAttribute(key, entity);
|
||||
}
|
||||
|
||||
public static Object getRequestAttribute(String key) {
|
||||
return getRequest().getAttribute(key);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user