`
newleague
  • 浏览: 1492194 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

使用web.xml方式加载Spring时,获取Spring context的两种方式:

阅读更多

1、servlet方式加载时:

web .xml】

 

Xml代码 复制代码
  1. <servlet>  
  2.         <servlet-name>dispatcherServlet</servlet-name>  
  3.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.         <init-param>  
  5.             <param-name>contextConfigLocation</param-name>  
  6.             <param-value>/WEB-INF/applicationContext</param-value>  
  7.         </init-param>  
  8.     </servlet>  

 

 

【jsp/servlet】

Java代码 复制代码
  1. ServletContext context = getServletContext();    
  2. XmlWebApplicationContext applicationContext = (XmlWebApplicationContext)   
  3.   
  4.   
  5. context.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet");   
  6.   
  7.   
  8.   DataSource dataSource=(DataSource)applicationContext.getBean("dataSource");  

 

 

 

 

2、listener方式加载时:

web .xml】

 

Java代码 复制代码
  1. <context-param>   
  2.   <param-name>contextConfigLocation</param-name>   
  3.   <param-value>/WEB-INF/applicationContext</param-value>   
  4.  </context-param>   
  5.   
  6. <listener>   
  7.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  8.  </listener>  

【jsp/servlet】

 

 

Java代码 复制代码
  1. ServletContext context = getServletContext();   
  2.   
  3.       
  4.    WebApplicationContext applicationContext  = WebApplicationContextUtils   
  5.      .getWebApplicationContext(context);   
  6.       
  7.   
  8.   
  9.   DataSource dataSource=(DataSource)applicationContext.getBean("dataSource"); 
分享到:
评论

相关推荐

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

    `&lt;listener&gt;`标签中的`&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;`定义了一个监听器,它会在Web应用启动时自动加载默认的Spring配置文件,即`/WEB-INF/...

    web.xml加载顺序与web.xml常用节点解析

    可以通过使用`&lt;listener&gt;`来加载Spring,而不是传统的`&lt;context-param&gt;`配合`ContextLoaderListener`。 - 遵循`web.xml`的规范和约定,确保配置文件的正确性和可读性,避免因顺序问题导致的部署失败。 了解`web....

    java解决org.springframework.web.context.ContextLoaderListener

    在Java Web开发中,`org.springframework.web.context.ContextLoaderListener` 是Spring框架的一部分,它负责初始化一个Web应用程序的Spring上下文。这个监听器是基于Servlet容器(如Tomcat、Jetty等)的,当Web应用...

    详解Spring mvc的web.xml配置说明

    在Spring MVC中,`org.springframework.web.context.ContextLoaderListener`是一个关键监听器,它实现了`ServletContextListener`接口。当Web应用程序启动时,Tomcat或Jetty等容器会触发此监听器,进而加载Spring的...

    SpringMVC基于代码的配置方式(零配置,无web.xml)

    传统的SpringMVC配置往往依赖于XML文件,如web.xml和spring-servlet.xml等,但随着Spring框架的发展,出现了基于代码的配置方式,实现了零XML配置,提高了开发效率。本文将详细介绍如何在不使用web.xml的情况下,...

    web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序

    在`web.xml`中配置Spring时,通常通过Listener来加载Spring容器,这样可以在应用程序启动时初始化所有的Spring Bean。 **示例代码:** ```xml org.springframework.web.context.ContextLoaderListener ...

    spring在web.xml中和在struts中的不同配置..pdf

    在本文中,我们将探讨Spring在`web.xml`中的配置与在Struts中的配置差异,以及这两种配置方式背后的基本原理。 首先,Spring的核心是ApplicationContext,它是一个管理Bean的容器,可以看作是应用程序的上下文环境...

    struts.xml和applicationContext.xml、web.xml的配置

    &lt;filter-class&gt;org.springframework.web.context.ContextLoaderFilter &lt;filter-name&gt;contextLoaderFilter &lt;url-pattern&gt;/* ``` 这部分配置将Struts2的过滤器和Spring的上下文加载过滤器映射到所有的URL,确保...

    解析web.xml中在Servlet中获取context-param和init-param内的参数

    在Java Web开发中,`web.xml`是应用的部署描述符,它包含了应用程序的各种配置信息。其中,`context-param`和`init-param`是两个重要的元素,用于设置应用级和Servlet级的初始化参数。理解它们的用法和如何在Servlet...

    web.xml 配置大全

    在实际项目中,为了提高可维护性和灵活性,可能会使用Spring的`@WebServlet`、`@WebFilter`和`@WebListener`注解替代部分`web.xml`配置,或者采用Java Config方式来配置应用程序。但`web.xml`仍然保留其核心地位,...

    spring bean XML配置入门

    一旦XML配置加载到Spring容器中,容器将根据配置创建Bean实例,并按照定义进行初始化、依赖注入,最后完成Bean的生命周期管理。 10. **实践操作**: 在实际开发中,我们可以使用Eclipse的Spring插件来简化Bean...

    spring在web.xml中和在struts中的不同配置.[收集].pdf

    本篇文章将深入探讨Spring在`web.xml`中与在Struts中的不同配置方式,以及这两种方式背后的设计思想。 首先,ApplicationContext是Spring的核心组件,它是一个容器,负责管理和装配应用程序中的Bean。在Web应用中,...

    SSH和SSI等框架常用基础配置web.xml

    对于使用SSH(Struts + Spring + Hibernate)和SSI(Struts + Spring + iBatis)等框架的应用程序而言,合理的`web.xml`配置至关重要。本文将详细介绍`web.xml`中的关键配置项,并解释其作用。 #### 一、`web.xml`...

    springmvc-config.xml

    这是一个springmvc-config.xml文件,&lt;?xml version="1.0" encoding="UTF-8"?&gt; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:...

    Spring在web.xml中的配置详细介绍

    首先,要理解Spring在web应用中主要通过两种方式提供上下文(Context)加载器:一种是基于Listener接口实现的ContextLoaderListener,另一种是基于Servlet接口实现的ContextLoaderServlet。这两种方式在功能上是相同...

    web.xml中的listen

    - **加载Spring配置文件**:这主要是通过`org.springframework.web.context.ContextLoaderListener`类实现的。该类是Spring框架提供的一个监听器类,用于在Web应用启动时加载Spring的应用上下文(ApplicationContext...

    web.xml中ContextLoaderListener的运行过程解析

    6. **处理请求**:Web应用开始接收HTTP请求,Servlet、Filter和其他监听器可以在需要时通过`ServletContext`获取ApplicationContext,进而访问和操作Spring Bean。 ### 5. `ContextLoaderListener`与`...

    快速搭建一个java config(无web.xml)的web工程(二)

    在Java开发领域,Web应用程序的配置方式有很多种,其中一种是使用Java Config,它提供了一种无需XML配置的方式来创建和管理Spring框架中的bean。本篇文章将详细介绍如何快速搭建一个基于Java Config的Web工程,该...

    快速搭建一个java config(无web.xml)的web工程(一)

    总结一下,通过使用Java Config,我们无需web.xml就能配置和启动一个Spring Web应用。Spring Boot的自动配置特性极大地简化了这个过程,使得开发者可以更专注于业务逻辑,而不是繁琐的配置工作。在本文中,我们创建...

Global site tag (gtag.js) - Google Analytics