The Spring environment gets the Spring Bean
1. Test data preparation
/* 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 employees -- ---------------------------- INSERT INTO `employee` VALUES (1, 'Zhang San', '[email protected]', 28, 101); INSERT INTO `employee` VALUES (2, 'Wang Wu', '[email protected]', 24, 102); SET FOREIGN_KEY_CHECKS = 1;
Two, SpringContextUtil writing tool class
The tool class written is SpringUtil, which implements the ApplicationContextAware interface, and adds Component annotations to let spring scan the 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 { /** * Context object instance */ private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this. applicationContext = applicationContext; } /** * Get applicationContext * * @return */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * Get 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"); } /** * Get the Bean by name. * * @param name * @return */ public static Object getBean(String name) { return getApplicationContext().getBean(name); } /** * Get Bean by class. * * @param clazz * @param <T> * @return */ public static <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } /** * Return the specified Bean by name and Clazz * * @param name * @param clazz * @param <T> * @return */ public static <T> T getBean(String name, Class<T> clazz) { return getApplicationContext().getBean(name, clazz); } }
Three, SpringContextUtil tool class instance test
(1). Write a test mapper instance:
/** * @Project: * @Description: * @Auther: songwp * @Date: 2022/1/30 10:23 **/ @Mapper public interface EmployeeMapper { List<Employee> findAll(); Employee selectById(Integer id); }
(2) The junit unit test results are as follows:
@Test public void contextLoads(){ EmployeeMapper empService = SpringContextUtil.getBean(EmployeeMapper.class); System.out.println(empService.findAll()); }
0 Comments