SpringBoot之获取Spring容器上下文组件的工具类封装
文章目录
- SpringBoot之获取Spring容器上下文组件的工具类封装
- [1. SpringBoot版本](#1. SpringBoot版本)
- [2. Spring容器上下文工具类封装](#2. Spring容器上下文工具类封装)
- 如在一个普通类中获取
HttpServletRequest
、HttpServletResponse
...等等- 以工具类的形式getBen,而不需要
@Component、@Service、@Repository
或使用@Autowired
注入Ben等- 在使用的地方以静态方法获取对象即可
1. SpringBoot版本
xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
2. Spring容器上下文工具类封装
创建一个工具类,如
SpringContextUtils
,并实现ApplicationContextAware
接口
SpringContextUtils.java
配置类如下
java
package com.yuan.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
yuan
/**
* <p>
* Description: Spring上下文及核心包中一些公用的核心方法工具类
* </p>
*
* @author jinshengyuan
* @since 2022/12/17 20:20
*/
@Configuration
public class SpringContextUtils implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
/**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取HttpServletRequest
*/
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
/**
* 获取HttpServletResponse
*/
public static HttpServletResponse getHttpServletResponse() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
}
public static String getDomain() {
HttpServletRequest request = getHttpServletRequest();
StringBuffer url = request.getRequestURL();
return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
}
public static String getOrigin() {
HttpServletRequest request = getHttpServletRequest();
return request.getHeader("Origin");
}
/**
* 通过name获取 Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
/**
* 在普通类中获取StringRedisTemplate对象
*
* @return
*/
public static StringRedisTemplate getStringRedisTemplate() {
return (StringRedisTemplate) SpringContextUtils.getBean("stringRedisTemplate");
}
/**
* 在普通类中获取RedisTemplate对象
*
* @return
*/
public static RedisTemplate getRedisTemplate() {
return (RedisTemplate) SpringContextUtils.getBean("redisTemplate");
}
/**
* 在普通类中获取JdbcTemplate对象
*
* @return
*/
public static JdbcTemplate getJdbcTemplate() {
return (JdbcTemplate) SpringContextUtils.getBean("jdbcTemplate");
}
/**
* 获取项目的绝对路径
*
* @return
*/
public final static String getProjectClasspath() {
return ClassUtils.getDefaultClassLoader().getResource("").getPath();
}
}