课时10:Spring开发Web项目

1)Spring开发Web项目

  1.在java程序的入口是统一的main(),因此只需要在main中 实例化一次applicationContext.xml,就可以让springioc初始化

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");    

  2.在java web程序如何初始化springioc呢?

    2.1 思路:当服务(tomcat)启动时,通过监听器将springioc初始化一次

    2.2 监听器:监听tomcat服务启动,一旦启动 立刻实例化一个ioc容器对象(这个监听器不需要自己写,spring-web-4.3.9.RELEASEjar提供了) 因此spring开发web至少需要7个jar :spring-java的以及spring-web

注意:spring-web-4.3.9.RELEASE.jar要放在lib下面才能生效

      2.2.1 导入spring-web-4.3.9.RELEASE.jar目的实现监听器

      2.2.2 web项目启动,会自动加载web.xml,因此需要在web.xml中加载监听器(ioc容器初始化)

  <!--  指定Ioc容器的位置-->
  <context-param>
    <!--    监听器的父类ContextLoader中有一个属性contextConfigLocation  来指定配置的ioc容器-->
    <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>

猜你喜欢

转载自www.cnblogs.com/thisHBZ/p/12512019.html
今日推荐