Spring环境获取Spring的Bean
一、测试数据准备
/* Navicat Premium Data Transfer Source Server : swp-mysql Source Server Type : MySQL Source Server Version : 50730 Source Host : localhost:3306 Source Schema : project Target Server Type : MySQL Target Server Version : 50730 File Encoding : 65001 Date: 24/07/2022 23:56:58 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for employee -- ---------------------------- DROP TABLE IF EXISTS `employee`; CREATE TABLE `employee` ( `id` int(5) NOT NULL, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `age` int(3) NULL DEFAULT NULL, `dept_id` int(5) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of employee -- ---------------------------- INSERT INTO `employee` VALUES (1, '张三', 'zhangsan@qq.com', 28, 101); INSERT INTO `employee` VALUES (2, '王五', 'wangwu@qq.com', 24, 102); SET FOREIGN_KEY_CHECKS = 1;
二、SpringContextUtil编写工具类
编写的工具类为SpringUtil,实现ApplicationContextAware接口,并加入Component注解,让spring扫描到该bean:
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; import javax.servlet.http.HttpServletRequest; @Component public class SpringContextUtil implements ApplicationContextAware { /** * 上下文对象实例 */ private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * 获取applicationContext * * @return */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取HttpServletRequest */ public static HttpServletRequest getHttpServletRequest() { return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); } 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); } }
三、SpringContextUtil工具类实例测试
(1).编写测试的mapper实例:
/** * @Project: * @Description: * @Auther: songwp * @Date: 2022/1/30 10:23 **/ @Mapper public interface EmployeeMapper { List<Employee> findAll(); Employee selectById(Integer id); }
(2)junit单元测试结果如下:
@Test public void contextLoads(){ EmployeeMapper empService = SpringContextUtil.getBean(EmployeeMapper.class); System.out.println(empService.findAll()); }
古今成大事者,不唯有超世之才,必有坚韧不拔之志!