zoukankan      html  css  js  c++  java
  • Spring集成web

    1导入坐标

        <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.5.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.0.5.RELEASE</version>
            </dependency>

    2配置监听器 在web.xml中

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param> 
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

    3使用WebApplicationContext获取上下文

    public class UserServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext servletContext = this.getServletContext();
            ApplicationContext webapp = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            UserService userService=webapp.getBean(UserService.class);
            userService.save();
    
        }
    }
  • 相关阅读:
    windows禅道环境搭建
    python-django开发学习笔记四
    迭代器
    小数据池
    正则表达式
    文件操作
    深浅拷贝
    隐藏文件夹命令
    python解释器安装教程以及环境变量配置
    计算机基础应用
  • 原文地址:https://www.cnblogs.com/ziwang520/p/15798310.html
Copyright © 2011-2022 走看看