com.pongsky.kit.common.utils.SpringUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-boot-starter-common Show documentation
Show all versions of spring-boot-starter-common Show documentation
Java 增强套件 之 公共 Spring Boot Starter 模块
The newest version!
package com.pongsky.kit.common.utils;
import lombok.NonNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Spring 工具类
*
* @author pengsenhao
*/
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
/**
* Bean 工厂
*/
private static ConfigurableListableBeanFactory beanFactory = null;
/**
* 应用上下文
*/
private static ApplicationContext applicationContext = null;
@Override
public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException {
SpringUtils.beanFactory = beanFactory;
}
@Override
public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
}
/**
* 获取 Bean 工厂
*
* @return 获取 Bean 工厂
*/
public static ConfigurableListableBeanFactory getBeanFactory() {
return beanFactory;
}
/**
* 获取应用上下文
*
* @return 获取应用上下文
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取 HttpServletRequest
*
* @return 获取 HttpServletRequest
*/
public static HttpServletRequest getHttpServletRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return null;
}
return attributes.getRequest();
}
/**
* 获取 HttpServletResponse
*
* @return 获取 HttpServletResponse
*/
public static HttpServletResponse getHttpServletResponse() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return null;
}
return attributes.getResponse();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy