后端java开发

web应用使用spring

2021-02-28  本文已影响0人  墨线宝

原文链接http://zhhll.icu/2021/%E6%A1%86%E6%9E%B6/spring/web%E5%BA%94%E7%94%A8%E4%BD%BF%E7%94%A8spring/

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>

由于本身的博客百度没有收录,博客地址http://zhhll.icu

上一篇 下一篇

猜你喜欢

热点阅读