Spring学习:四、 在web开发中使用spring框架

迷南。 2023-10-04 20:17 110阅读 0赞

6. 在web开发中使用spring框架

  1. 创建一个web项目

在这里插入图片描述
>\[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lXyPOVPl-1670413867380)(assets/image-20220801192505321.png)\]在这里插入图片描述

  1. 导入相关依赖

    <?xml version=”1.0” encoding=”UTF-8”?>

    4.0.0

    org.example
    spring-webdemo
    1.0-SNAPSHOT

    war

    spring-webdemo Maven Webapp

    http://www.example.com
    UTF-8
    1.8
    1.8
    5.3.21


    org.springframework
    spring-context
    ${spring.version}


    org.springframework
    spring-test
    ${spring.version}



    org.springframework
    spring-web
    ${spring.version}



    javax.servlet
    javax.servlet-api
    3.1.0
    provided



    javax.servlet.jsp
    jsp-api
    2.1
    provided


    junit
    junit
    4.12
    test




    org.slf4j
    slf4j-log4j12
    1.7.36



    org.projectlombok
    lombok
    1.18.24



    javax.inject
    javax.inject
    1

  1. <build>
  2. <finalName>spring-webdemo</finalName>
  3. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  4. <plugins>
  5. <plugin>
  6. <artifactId>maven-clean-plugin</artifactId>
  7. <version>3.1.0</version>
  8. </plugin>
  9. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  10. <plugin>
  11. <artifactId>maven-resources-plugin</artifactId>
  12. <version>3.0.2</version>
  13. </plugin>
  14. <plugin>
  15. <artifactId>maven-compiler-plugin</artifactId>
  16. <version>3.8.0</version>
  17. </plugin>
  18. <plugin>
  19. <artifactId>maven-surefire-plugin</artifactId>
  20. <version>2.22.1</version>
  21. </plugin>
  22. <plugin>
  23. <artifactId>maven-war-plugin</artifactId>
  24. <version>3.2.2</version>
  25. </plugin>
  26. <plugin>
  27. <artifactId>maven-install-plugin</artifactId>
  28. <version>2.5.2</version>
  29. </plugin>
  30. <plugin>
  31. <artifactId>maven-deploy-plugin</artifactId>
  32. <version>2.8.2</version>
  33. </plugin>
  34. </plugins>
  35. </pluginManagement>
  36. </build>
  37. </project>
  1. 在web.xml文件中配置监听器,用于监听web服务器的启动, 创建Spring容器

    1. <!--spring监听器-->
    2. <listener>
    3. <listener-class>
    4. org.springframework.web.context.ContextLoaderListener
    5. </listener-class>
    6. </listener>

    如果没有配置监听器,那么Spring默认加载 WEB-INF/applicationContext.xml,而我们的spring的配置文件是位于

在这里插入图片描述

使用<context-param>配置spring的配置文件路径:

  1. <!--指定spring的配置文件-->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:applicationContext.xml</param-value>
  5. </context-param>

通过上面配置的spring的监听器,那么在tomcat启动时,spring工厂进行创建 !

  1. 创建Servlet,在Servlet中获取Spring容器对象,通过getBean()获取service层的对象

    package com.suke.web;

    import com.suke.service.UserService;
    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(name = “UserServlet”,value = “/userServlet”)
    public class UserServlet extends HttpServlet {

    1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  1. }
  2. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  3. //1.第一种方式:
  4. //ApplicationContext applicationContext = (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  5. //2.第二种方式
  6. ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
  7. UserService userService = applicationContext.getBean("userService", UserService.class);
  8. userService.addUser();
  9. }
  10. }

发表评论

表情:
评论列表 (有 0 条评论,110人围观)

还没有评论,来说两句吧...

相关阅读