3111人加入学习
(9人评价)
SSM框架第二季 - Spring入门

制作完成于2018-09-06 使用Eclipse-4.7.3a、JDK8、Spring-5.0.8、Mybatis-3.4.6

价格 免费

每次请求都会创建一个新的容器,但是在web项目中我们只需要一个spring的容器。所以我们开始了“配置Spring随项目启动”:

 

[展开全文]

解决web项目中只需要一个spring容器

利用ServletContextListener 通过配置监听器来打到我们的需求,在web项目创建时创建spring容器,销毁时候关闭spring容器

 

在web.xml中配置 监听器,

  <!-- 配置监听器,在web项目启动的时候让spring启动 -->

<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener.class</listener-class>
  </listener>

  <!-- 读取spring配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

 

在UserLoginServlet中:

        //ServletContextListener 可以通过配置监听器来打到我们的需求,在web项目创建时创建spring容器,销毁时候关闭spring容器
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        us = (UserService)wac.getBean("userService");
        

[展开全文]

授课教师

SIKI学院老师

课程特色

下载资料(1)
视频(36)