Spring集成Web环境

Spring集成web环境

1.首先创建一个最简单的maven项目,不要用原型
2.在项目结构中,对模块添加web选项,选择好web-inf和xml部署的位置
https://blog.csdn.net/qq_37112085/article/details/108303613?utm_term=maven%E9%A1%B9%E7%9B%AE%E5%A6%82%E4%BD%95%E6%B7%BB%E5%8A%A0webapp&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-1-108303613&spm=3001.4430
3.webapp中添加index.jsp文件
4.pom.xml中添加依赖

pom.xml中添加依赖
<!--添加web层组件-->
<dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <version>3.0.1</version>
       <scope>provided</scope>
</dependency>
<dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.2.1</version>
        <scope>provided</scope>
</dependency>

5.编辑配置好tomat

避免多次加载配置文件(读取上下文对象ApplicationContext)

在Web项目中,可以使用ServletContextListener监听Web应用的启动
在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中
这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。

自定义ContextLoaderListener获取应用上下文
web.xml

<!--配置监听器-->
    <listener>
        <listener-class>com.itheima.listener.ContextLoaderListener</listener-class>
    </listener>

    <!--全局初始化参数-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>applicationContext.xml</param-value>
    </context-param>
    <!--当使用其他名称配置文件,只要修改web.xml内容即可-->

UserServlet

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = this.getServletContext();
        //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        ApplicationContext app = WebApplicationContextUtil.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

WebApplicationContextUtil

/*
解除ContextLoaderListener中
servletContext.setAttribute("app",app)
和UserServlet
ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
耦合问题
*/
/*
将其中servletContext传过来
单独创建工具类解决,修改UserServlet代码
 */
public class WebApplicationContextUtil {
    public  static ApplicationContext getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("app");
    }
}

ContextLoaderListener

public class ContextLoaderListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        //读取web.xml中的全局参数
       String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        ApplicationContext app=new ClassPathXmlApplicationContext(contextConfigLocation);
        //将Spring的应用上下文对象存储到ServletContext域中
        servletContext.setAttribute("app",app);
        System.out.println("spring容器创建完毕");
    }
    public void contextDestroyed(ServletContextEvent servletContextEvent) {}
}

Spring提供获取应用上下文工具
Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装
使用步骤

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.5.RELEASE</version>
</dependency>
  • 使用WebApplicationContextUtils获取应用上下文对象ApplicationContext
    web.xml文件中先配置
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 <!--全局初始化参数-->
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
</context-param>

UserServlet中直接使用

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext=this.getServletContext();
        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容