自学内容网 自学内容网

web应用使用spring

web应用使用spring

对于java而言,使用最多的还是web开发,如何在web应用中使用spring呢,web应用中没有main方法,而且必须在servlet容器加载时就创建spring的IOC容器,之前在学习Servlet的时候有讲到一个Servlet监听器,可以监听ServletContext、HttpSession、ServletRequest等对象的创建和销毁

所以可以实现ServletContextListener接口,重写contextInitialized方法来实现spring的IOC容器的创建

public interface ServletContextListener extends EventListener {
    void contextInitialized(ServletContextEvent var1);

    void contextDestroyed(ServletContextEvent var1);
}

在web.xml中配置listener

<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>

https://zhhll.icu/2021/框架/spring/基础/10.web应用使用spring/

本文由 mdnice 多平台发布


原文地址:https://blog.csdn.net/Lxn2zh/article/details/137993477

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!