Spring 如何整合 Servlet 的详细讲解

本文详细介绍了如何将Spring与Servlet整合,包括所需Jar包依赖、环境搭建步骤,如创建Web项目、配置Spring启动监听器以及在Servlet中获取Bean对象。通过实例展示了业务层、Servlet和持久层的创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值