Spring整合Junit、Servlet
整合Junit
使用的jar包
创建项目
创建配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.bjsxt"/>
</beans>
创建业务层代码
package com.bjsxt.service;
public interface UsersService {
void addUsers();
}
import com.bjsxt.service.UsersService;
import org.springframework.stereotype.Service;
@Service
//配置注解,使得配置文件可以扫描到
public class UsersServiceImpl implements UsersService {
@Override
public void addUsers() {
System.out.println("AddUsers.....");
}
}
创建测试类
import com.bjsxt.service.UsersService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//1. 更换测试引擎
@RunWith(SpringJUnit4ClassRunner.class)
//2. 指定Spring配置文件
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UsersServiceImplTest {
//3. 使用依赖注入,在测试类内注入要测试的对象
@Autowired
private UsersService usersService;
//4. 在测试方法内完成对测试方法的测试
@Test
public void testAddUsers(){
this.usersService.addUsers();
}
}
整合Servlet
使用的jar包
创建项目
如果是web工程的话,我们还需要将添加的jar包都导入到项目的lib下
创建配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
在 Web 项目中启动 Spring 框架
在 Web 项目中需要在 web.xml 文件中配置启动 Spring 框架的监听器。用于启动 Spring 框架。
修改 web.xml 文件
<?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>
</web-app>
在 Servlet 中获取 Bean 对象
创建业务层
package com.bjsxt.service;
public interface UsersService {
void addUsers();
}
package com.bjsxt.service.impl;
import com.bjsxt.service.UsersService;
import org.springframework.stereotype.Service;
@Service
public class UsersServiceImpl implements UsersService {
@Override
public void addUsers() {
System.out.println("AddUsers......");
}
}
创建 Servlet
package com.bjsxt.web.servlet;
import com.bjsxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/addUsers.do")
public class UsersServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//如何在servlet内获取springIOC容器中的bean对象
//方式一 通过ServletContext对象获取WebApplicationContext,之后通过它的getBean方法获取bean对象
/* WebApplicationContext webApplicationContext = (WebApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
UsersService usersService = (UsersService)webApplicationContext.getBean("usersServiceImpl");
resp.getWriter().print("hello welcome");
usersService.addUsers();*/
//方式二 通过工具类获取WebApplicationContext对象
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
UsersService usersService = (UsersService)webApplicationContext.getBean("usersServiceImpl");
resp.getWriter().print("hello welcome");
usersService.addUsers();
}
}
配置文件 applicationContext.xml的注解扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.bjsxt.service"/>
</beans>
运行结果:
创建持久层
package com.bjsxt.dao;
public interface UsersDao {
void insertUsers();
}
package com.bjsxt.dao.impl;
import com.bjsxt.dao.UsersDao;
import org.springframework.stereotype.Repository;
@Repository
//使用@Repository 将持久层注入到业务层内
public class UsersDaoImpl implements UsersDao {
@Override
public void insertUsers() {
System.out.println("insert into users....");
}
}
在业务层的UsersServiceImpl类的方法内添加和持久层有关的成员变量
并使用@Autowired完成对于对象的注入
package com.bjsxt.service.impl;
import com.bjsxt.dao.UsersDao;
import com.bjsxt.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersDao usersDao;
@Override
public void addUsers() {
System.out.println("AddUsers......");
this.usersDao.insertUsers();
}
}
修改 Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.bjsxt.service,com.bjsxt.dao"/>
</beans>
再次运行,运行的结果为: