`
icomparator
  • 浏览: 18753 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Web项目容器启动就初始化Spring容器的方式。

 
阅读更多
初始化方式一:

servlet中init方法中完成。但是init方法在第一次访问的时候才执行。


@Override

public void init() throws ServletException {


System.out.println("Servlet初始化,并且初始化Spring");

//通过spring容器获取一个bean对象

//第一步初始化spring容器(工厂)

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


//把spring放到web的application对象中。

getServletContext().setAttribute("applicationContext",applicationContext);


//其他的servlet中.

getServletContext().getAttribute("applicationContext");

}

 

  初始化方式二:
  通过web listeners
  ServletContextListener ->容器启动过程中调用该监听器。
  
  [  web.xml  ]
  	  <!--
    	 配置spring初始化容器的监听器
    	  监听器:创建applicationContext对象并且放到servletContext中.
    -->
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    
    <!--
    	 配置spring的配置文件路径,如果不写默认使用WEB-INF/applicationContext.xml 
    -->
    <context-param>
    	 <param-name>contextConfigLocation</param-name>
    	 <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
        
      代码中使用:
      AppliationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
      context.getBean(beanId);

  

 

 

分享到:
评论

相关推荐

    Spring源代码解析(二):IoC容器在Web容器中的启动.doc

    当Web容器启动时,Spring的初始化过程会创建`XmlWebApplicationContext`实例,并调用`refresh()`方法来加载bean定义。这个过程主要由`loadBeanDefinitions()`方法完成,它使用`XmlBeanDefinitionReader`来解析XML...

    启动Spring项目详细过程(小结)

    该过程可以分为四个步骤:项目放到web项目容器中、容器启动时加载读取web.xml配置文件、ContextLoaderListener中的contextInitialized()方法、初始化spring容器。 步骤1:项目放到web项目容器中 首先,我们需要将...

    Java Web项目加入Spring框架

    同时,也需要配置ContextLoaderListener,它会在应用启动时初始化Spring容器。 3. **创建Spring配置文件**:在`src/main/resources`目录下创建一个名为`applicationContext.xml`的文件,用于定义Bean的配置。这里...

    Spring框架web项目实战全代码分享

    - `&lt;listener&gt;`:定义了一个`ContextLoaderListener`监听器,当Web应用启动时,它会读取`context-param`中指定的配置文件,初始化Spring应用上下文。 4. **创建`applicationContext.xml`**: 这是Spring的配置...

    Spring在容器在启动的时候发生了什么

    在Spring框架中,当一个基于Servlet的Web应用启动时,Spring容器的初始化过程是至关重要的。这个过程涉及到多个组件和步骤,让我们详细探讨一下。 首先,我们在`web.xml`配置文件中看到了`&lt;context-param&gt;`和`...

    Spring3.1.3 Ioc在Web容器中的建立

    标题 "Spring3.1.3 Ioc在Web容器中的建立" 涉及...这个过程涉及到Spring容器的初始化、bean的定义与依赖注入,以及Web应用的结构配置。通过理解和熟练运用这些知识,开发者可以更好地构建可维护、可扩展的Spring应用。

    spring_boot_web 微服务项目

    1. 初始化项目:使用Spring Initializr创建新的Spring Boot项目,选择`spring-boot-starter-web`起步依赖。 2. 创建RESTful API:定义Controller,使用@RequestMapping和@GetMapping、@PostMapping等注解定义HTTP...

    获取spring容器的方法

    这样,在Spring容器启动时,它会自动调用`setApplicationContext`方法,将`ApplicationContext`实例注入到实现了`ApplicationContextAware`的类中。 ### 结论 选择哪种方法获取Spring容器主要取决于具体的应用场景...

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    在`web.xml`中添加`ContextLoaderListener`监听器,以初始化Spring应用上下文: ```xml &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener ``` 3. **指定Spring配置文件位置**: 在...

    web监听端口(Socket服务随web启动而启动)

    3. **Spring框架的ApplicationListener**:如果你的Web应用使用Spring框架,可以实现`ApplicationListener`接口,监听`ContextRefreshedEvent`,在Spring容器初始化完成后启动Socket服务。 4. **使用后台线程**:在...

    Spring 自启动项目demo

    通过这个Spring自启动项目demo,我们可以学习到Spring如何管理和初始化Bean,以及如何使用Spring MVC来构建一个简单的Web应用。这只是一个基础的示例,实际项目中可能涉及到更复杂的配置和功能,如AOP(面向切面编程...

    Java Web项目的完整案例概述和Spring boot部分代码示例.zip

    3. **Spring Boot**:Spring Boot是Spring的微服务框架,它预设了许多默认配置,减少了XML配置和初始化工作。Spring Boot支持自动配置、嵌入式Web服务器(如Tomcat)、健康检查和Actuator等特性,极大地提高了开发...

    ioc容器在Web容器中的启动.pdf

    总结来说,Spring IOC容器在Web容器中的启动涉及到创建`WebApplicationContext`,加载配置文件中的bean定义,然后通过`refresh()`方法实例化和初始化bean。这个过程确保了Web应用程序能够正确地配置和运行其依赖的...

    Spring定时任务(Web项目)

    1. 定时任务通常在Spring容器启动后立即运行,因此需要确保Spring容器在Web应用启动时已完成初始化。 2. 如果定时任务涉及数据库操作,需要确保数据库连接已经建立并可用。 3. 为了防止并发执行,可使用`@...

    加载spring 文件,在web.xml中的配置

    `&lt;servlet&gt;`标签内的`&lt;servlet-class&gt;org.springframework.web.context.ContextLoaderServlet&lt;/servlet-class&gt;`则定义了一个Servlet,它同样用于初始化Spring容器。与`ContextLoaderListener`不同的是,...

    springBean加载过程源码解析文档,附有代码类名和行数

    Spring Bean 加载过程是 Spring 框架中最核心的部分之一,涉及到 ApplicationContext 的初始化、Bean 的加载和注册等过程。在 Spring Boot 应用程序中,SpringApplication 负责加载和管理 Bean。 SpringApplication...

    spring无web.xml零配置

    Spring Boot通过“起步依赖”(Starter POMs)和自动配置(Auto Configuration)简化了应用的初始化。只需在`pom.xml`中添加相应的依赖,Spring Boot就能根据类路径中的存在情况自动配置相关功能。 例如,为了使用...

    Spring在web下启动流程学习笔记

    这个监听器是Spring Web应用程序的核心组件,它继承自`ContextLoader`,并在Web容器启动时执行`contextInitialized`方法。在这个方法中,`ContextLoader`会调用`initWebApplicationContext`,这是启动Web上下文的...

    Spring之核心容器bean

    在Spring中,bean是一个由容器管理的对象,它的实例化、初始化、装配和销毁都是由Spring容器控制的。你可以通过XML、注解或者Java配置类来定义bean。例如,一个简单的XML配置如下: ```xml ``` 这里,`myBean`...

    Spring容器 .ppt

    Spring容器是Spring框架的核心组成部分,它负责管理对象的生命周期和对象之间的依赖关系。Spring容器的主要职责是读取配置元数据,创建和组装Bean,并提供Bean的完整生命周期管理。本篇内容将深入探讨Spring容器的...

Global site tag (gtag.js) - Google Analytics