3. Spring集成Junit
3.3 Spring集成Junit步骤
①导入spring集成Junit的坐标②使用@Runwi th注解替换原来的运行期③使用@ContextConfiguration指定配置文件或配置类④使用@Autowi red注入需要测试的对象⑤创建测试方法进行测试
packagecom.jian.test;
importcom.jian.config.SpringConfiguration;
importcom.jian.service.UserService;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
importjavax.sql.DataSource;
importjava.sql.SQLException;
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:applicationContext.xml")
@ContextConfiguration(classes= {SpringConfiguration.class}) //全注解的方式即使用核心配置类来配置文件
publicclassSpringJunitTest {
@Autowired
privateUserServiceuserService;
@Autowired
privateDataSourcedataSource;
@Test
publicvoidtest1() throwsSQLException {
userService.save();//调用sava方法
System.out.println(dataSource.getConnection());
}
}
1. Spring集成web环境
1.1 ApplicationContext应用上下文获取方式
应用上下文对象是通过
new ClasspathXmIApplicationContext(spring配置文件)方式获取的,但是每次从容器中获得Bean时都要编写
new ClasspathXmIApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次。
在Web项目中,可以使用
ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Sprfnig的配置文件,创建应用上下文对象
ApplicationContext,在将其存储到最大的域
servletContext域中,这样就可以在任意位置从域中获得应用上下文
ApplicationContext对象了。
1.2 Spring提供获取应用上下文的工具
上面的分析不用手动实现,Spring提供了一个监听器
GentextLoaderListener就是对上述功能的封装,该监听内部加载Spring配置文件,创建应用上下文对象,并存储到
ServletContext域中,提供了一个客户端工具
WebApplicationContextUtils供使用者获得应用上下文对象。
所以我们需要做的只有两件事:
在
web.xml中配置
ContextLoaderListener监听器(导入spring-web坐标)
使用
WebApplicationContextUtils获得应用上下文对象
ApplicationContext
1.3 导入Spring集成web的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.23</version>
</dependency>
1.4 配置ContextLoaderListener监听器
<!--全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
1.5 通过工具获取应用上下文对象
ApplicationContextaphplicationcontext=
WebApplicationContextUtils.getWebapplicationcontext(servletContext);objectobj=applicationcontext.getBean ( "id" ) ;
1.5 知识要点
Spring集成web环境步骤
配置ContextLoaderListener监听器
使用WebApplicationContextUtils获得应用上下文
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.23</version>
</dependency>
packagecom.jian.web;
importcom.jian.service.UserService;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.springframework.web.context.WebApplicationContext;
importorg.springframework.web.context.support.WebApplicationContextUtils;
importjavax.servlet.ServletContext;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.io.IOException;
publicclassUserServletextendsHttpServlet {
@Override
protectedvoiddoGet(HttpServletRequestreq, HttpServletResponseresp) throwsServletException, IOException {
//ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
ServletContextservletContext=this.getServletContext();
//ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
//ApplicationContext app=WebApplicationContextUtils.getWebApplicationContext(servletContext);
/* WebApplicationContext*/ApplicationContextapp=WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserServiceuserService=app.getBean(UserService.class);
userService.save();
}
}
packagecom.jian.listener;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importjavax.servlet.ServletContext;
importjavax.servlet.ServletContextEvent;
importjavax.servlet.ServletContextListener;
publicclassContextLoaderListenerimplementsServletContextListener {
publicvoidcontextInitialized(ServletContextEventservletContextEvent) {
ServletContextservletContext=servletContextEvent.getServletContext();
//读取web.xml中的全局参数
StringcontextConfigLocation=servletContext.getInitParameter("contextConfigLocation");
ApplicationContextapp=newClassPathXmlApplicationContext(contextConfigLocation);
//将Spring的应用上下文对象储存到ServletContext域中
servletContext.setAttribute("app",app);
System.out.println("spring容器创建完毕。。。");
}
publicvoidcontextDestroyed(ServletContextEventservletContextEvent) {
}
}
packagecom.jian.listener;
importorg.springframework.context.ApplicationContext;
importjavax.servlet.ServletContext;
publicclassWebApplicationContextUtils {
publicstaticApplicationContextgetWebApplicationContext(ServletContextservletContext){
return (ApplicationContext) servletContext.getAttribute("app");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置Dao-->
<bean id="userDao" class="com.jian.dao.Impl.UserDaoImpl"/>
<!--配置service-->
<bean id="userService" class="com.jian.service.Impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
<?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">
<!--全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.jian.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
</web-app>
package com.jian.dao;
public interface UserDao {
public void save();
}
package com.jian.dao.Impl;
import com.jian.dao.UserDao;
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("save running...");
}
}
package com.jian.service;
public interface UserService {
public void save();
}
package com.jian.service.Impl;
import com.jian.dao.UserDao;
import com.jian.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao=userDao;
}
public void save() {
userDao.save();
}
}