五、Spring集成Junit
1、原始Junit测试Spring的问题
在测试类中,每个测试方法都有以下两行代码
ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml"); UserService userService = app.getBean("userService",userService.class);
这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常,所以不能轻易删掉
2、上述问题的解决思路
1.让SpringJunit负责创建Spring容器,但是需要将配置文件的名称告诉它
2.将需要进行测试的Bean直接在测试类中注入
3、Spring集成Junit步骤
1.导入Spring集成Junit的坐标
2.使用@Runwith注解替换原来的运行期
3.使用@ContextConfiguration指定配置文件或配置类
4.使用Autowired注入需要测试的对象
5.创建测试方法进行测试
导入Spring集成Junit的坐标
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.12</version> </dependency>
@ContextConfiguration(classes = {SpringConfiguration.class}) @RunWith(SpringJUnit4ClassRunner.class) public class SpringJunitTest { @Autowired private UserService userService; @Autowired private DataSource dataSource; @Test public void test1() throws SQLException { userService.test(); System.out.println(dataSource.getConnection()); } }
六、Spring集成web环境
1、ApplicationContext应用上下文获取方式
应用上下文对象是通过new ClasspathXmlApplicationContext(Spring配置文件)方式获取的,但是每次从容器中获得Bean时都要new一次,这样就会导致配置文件被加载多次,应用上下文对象创建多次。
2、自定义ContextLoaderListener
在web项目中,可以使用ServletContextListener监听web应用的启动,我们可以在web应用启动时,就加载Spring配置文件,创建应用上下文对象ApplicationContext,再将其存储到最大的域ServletContext域中,这样就可以在任意位置从域中获得应用上下文对象了。
@WebListener public class ContextLoaderListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class); ServletContext servletContext = sce.getServletContext(); servletContext.setAttribute("app",app); System.out.println("监听ing"); } }
@WebServlet("/login") public class Login extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class); ApplicationContext app = (ApplicationContext) this.getServletContext().getAttribute("app"); UserService userService = app.getBean(UserService.class); userService.test(); } }
优化写法(解耦)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 将Spring配置类写在配置文件中解耦 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>cn.ken.config.SpringConfiguration</param-value> </context-param> </web-app>
@WebListener public class ContextLoaderListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { ServletContext servletContext = sce.getServletContext(); String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation"); try { Class aClass = Class.forName(contextConfigLocation); ApplicationContext app = new AnnotationConfigApplicationContext(aClass); servletContext.setAttribute("app",app); System.out.println("监听ing"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
// 将Spring容器的获取提取出来,则使用时不需要知道其保存在servletContext中的属性名 public class WebApplicationContextUtils { public static ApplicationContext getApplicationContext(ServletContext servletContext){ return (ApplicationContext) servletContext.getAttribute("app"); } }
@WebServlet("/login") public class Login extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); // 通过静态方法直接获取Spring容器应用上下文 // ApplicationContext app = (ApplicationContext) this.getServletContext().getAttribute("app"); ApplicationContext app = WebApplicationContextUtils.getApplicationContext(servletContext); UserService userService = app.getBean(UserService.class); userService.test(); } }
3、Spring提供获取应用上下文工具
上文中的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具webApplicationContextUtils供使用者获得应用上下文对象
所以我们需要做的只有两件事:
1.导入Spring-web坐标
2.在web.xml中配置ContextLoaderListener监听器
3.使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.12</version> </dependency>
//@WebServlet("/login") public class Login extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = this.getServletContext(); ApplicationContext app = (ApplicationContext) WebApplicationContextUtils.getWebApplicationContext(servletContext); UserService bean = (UserService) app.getBean(UserService.class); bean.test(); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true" > <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>login</servlet-name> <servlet-class>cn.ken.controller.Login</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app>