文章目录
Spring 整合 Servlet
一、 Jar 包依赖
Spring 核心容器模块:
spring-beans-5.2.7.RELEASE.jar
spring-context-5.2.7.RELEASE.jar
spring-core-5.2.7.RELEASE.jar
spring-expression-5.2.7.RELEASE.jar
Commons-Loggin 日志:
commons-logging-1.2.jar
Spring AOP 模块:
spring-aop-5.2.7.RELEASE.jar
SpringWeb 模块:
spring-web-5.2.7.RELEASE.jar
Servlet:
servlet-api.jar
二、 搭建环境
1、创建 Web 项目
2、添加 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">
</beans>
3、 在 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>
4、 在 Servlet 中获取 Bean 对象
创建业务层
public interface UsersService {
void addUsers();
}
@Service
public class UsersServiceImpl implements UsersService {
@Override
public void addUsers() {
System.out.println("AddUsers....");
}
}
创建 Servlet
@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 {
//方式一:
/* WebApplicationContext webApplicationContext = (WebApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
UsersService usersService = (UsersService) webApplicationContext.getBean("usersServiceImpl");
usersService.addUsers();*/
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
UsersService usersService = (UsersService) webApplicationContext.getBean("usersServiceImpl");
usersService.addUsers();
resp.getWriter().print("Hello Servlet");
}
}
创建持久层
public interface UsersDao {
void insertUsers();
}
@Repository
public class UsersDaoImpl implements UsersDao {
@Autowired
private UsersDao usersDao;
@Override
public void insertUsers() {
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>