`
onlyor
  • 浏览: 377335 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

ApplicationContext的三种实现方式以及在web.xml配置的两种方式

阅读更多

ApplicationContext 是 BeanFactory 接口的子接口,它增强了 BeanFactory 的功能,处于 context 包下。很多时候, ApplicationContext 允许以声明式方式操作容器,无须手动创建。可利用如 ContextLoader 的支持类,在 Web 应用启动时自动创建 ApplicationContext。当然,也可以采用编程方式创建ApplicationContext。

ApplicationContext包括BeanFactory的全部功能,因此建议优先使用ApplicationContext。除非对于某些内存非常关键的应用,才考虑使用 BeanFactory。

 

spring为ApplicationContext提供的3种实现分别为:

1、  ClassPathXmlApplicationContext:利用类路径的XML文件来载入Bean定义的信息

[1]  ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");

[2]  String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};

ApplicationContext ctx = new ClassPathXmlApplication(locations);

2、 FileSystemXmlApplicationContext:利用文件系统中的XMl文件来载入Bean

定义的信息

[1]  ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //加载单个配置文件

[2]  String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};

 ApplicationContext ctx = new FileSystemXmlApplicationContext(locations );

//加载多个配置文件

[3]  ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/project/bean.xml");

//根据具体路径加载

3、 XmlWebApplicationContext:从Web系统中的XML文件来载入Bean定义的信息。

 ServletContext servletContext = request.getSession().getServletContext();    

 ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

 

 

配置WebApplicationContext的两种方法:

(1)        利用Listener接口来实现

<listener>

       <listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

<context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:applicationContext</param-value>

</context-param>

(2)        利用Servlet接口来实现

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext</param-value>

</context-param>

<Servlet>

       <servlet-name>context</servlet-name>

       <servlet-class>

           org.springframework.web.context.ContextLoaderServlet

       </servlet-class>

</servlet>

 

分享到:
评论

相关推荐

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

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

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

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

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

    这两种方式在功能上是相同的,区别在于实现方式和版本兼容性。ContextLoaderListener适用于Servlet 2.3及以上版本,而ContextLoaderServlet适用于较早版本的Servlet API。 接下来,我们将详细解释如何在web.xml中...

    web.xml中如何设置配置文件的加载路径实例详解

    这种方式允许开发者在web应用程序启动时加载各种配置文件,包括日志配置、数据库连接信息以及Spring框架的配置文件等。以下是如何在web.xml中设置配置文件加载路径的详细说明。 首先,web应用程序启动时,容器如...

    SSH 整合原代码 包括2种方式:注解&xml;配置 (已测试, 原创)

    本资源提供的是SSH整合的原代码,涵盖了通过注解和XML配置两种方式进行整合的方法,并且已经过测试,确保其可行性和原创性。以下是关于SSH整合及这两种配置方式的详细知识: 1. **Struts框架**:Struts是一个基于...

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

    可以看出,有两种方法:一个是用 ContextLoaderListener 这个 Listerner,另一个是 ContextLoaderServlet 这个 Servlet,两者都是在 Web 应用启动的时候来初始化 WebApplicationContext。我个人认为 Listerner 要比 ...

    Spring中ApplicationContext加载机制

    首先,Spring 提供了两种选择来加载 ApplicationContext:ContextLoaderListener 和 ContextLoaderServlet。这两者在功能上完全等同,只是一个是基于 Servlet2.3 版本中新引入的 Listener 接口实现,而另一个基于 ...

    IDEA用maven创建springMVC项目和配置(XML配置和Java配置)

    本文将详细介绍如何在IntelliJ IDEA(IDEA)中使用Maven来创建一个Spring MVC项目,并探讨XML配置与Java配置两种方式。 首先,我们需要在IDEA中启动一个新的项目。选择“New Project”,然后在左侧选择"Maven",...

    Struts2+Hibernate4+Spring3整合(注解和XML方式都有)

    在"Struts2+Hibernate4+Spring3整合"中,通常有两种方式实现整合: 1. **XML方式**:这是传统的配置方式,所有的配置信息都在XML文件中。Struts2的配置文件(struts.xml)定义Action和结果,Hibernate的配置文件...

    Xfire配置Web Service+Spring+Hibernate详细流程

    以接口`IHello`和实现类`HelloImpl`为例,展示了如何在`applicationContext.xml`中注册bean,以及如何在`xfire-servlet.xml`中配置服务映射。 #### 结论 通过上述步骤,开发者可以成功地在项目中集成Web Service、...

    在Eclipse 中创建Spring的 Web应用.doc

    在Eclipse中创建一个基于Spring的Web应用涉及多个步骤,主要涵盖了Spring框架的Web模块、ApplicationContext的使用以及在Web容器中的配置。以下是详细的过程和相关知识点: 1. **Spring Web模块**: Spring框架...

    三大框架配置文件集合.rar

    Struts的配置文件主要有两个:`struts-config.xml`和`web.xml`。`struts-config.xml`定义了Action、ActionForm、ActionMapping等,用于处理用户请求。`web.xml`则负责设置过滤器、servlet等Web应用的全局配置。 ...

    Spring + struts 整合的三种主要方式

    本文将详细介绍Spring与Struts整合的三种主要方式:使用Spring的`ActionSupport`、使用Spring的`DelegatingRequestProcessor`类以及全权委托方式。 #### 1. 使用Spring的`ActionSupport` 使用Spring的`...

    dwr与spring集成的方式

    通过上述两种方式,可以实现DWR与Spring的有效集成,从而发挥两者的优点,提高系统的可维护性和扩展性。选择合适的方式取决于项目的具体需求和技术团队的习惯。对于已经熟悉Spring框架的开发者来说,第一种方式可能...

    java注册登录系统(xml&mysql)两种方式实现

    本资源提供了使用Java实现注册登录系统的方法,结合XML配置和MySQL数据库,我们将探讨这两种实现方式。 首先,Java是一种广泛使用的后端编程语言,特别适合构建服务器端逻辑。在注册登录系统中,Java主要负责处理...

    spring学习笔记

    Spring 的配置文件:..........................................................................................................7 1.3.4 在配置中配置类:........................................................

    WebService的几种不同实现方式

    虽然WebService被广泛认为是SOA的一个实现方式,但它并不是SOA的全部。SOA的理想状态是实现传输协议的透明化,而SOAP作为一个特有协议,并未完全达到这一点。 在Java中,主要存在三种WebService实现规范: 1. JAX-...

    ssh三大框架整合

    如果不希望在`struts-config.xml`中配置`ContextLoaderPlugIn`插件,可以在`web.xml`中配置一个监听器`org.springframework.web.context.ContextLoaderListener`来装载Spring上下文。 2. **特点**: - 不使用...

    Spring 和 struts 整合的三种方式

    配置Spring MVC时,需要在`web.xml`中配置DispatcherServlet,并在Spring的配置文件中定义Controller。例如: ```xml &lt;servlet-name&gt;dispatcher &lt;servlet-class&gt;org.springframework.web.servlet....

    Spring和strtus的xml文件的修改

    在探讨Spring与Struts框架中XML配置文件的修改这一主题时,我们首先需要理解这两个框架的基本概念以及它们是如何通过XML配置文件协同工作的。Spring框架是一个轻量级的控制反转(IoC)容器,用于管理Java应用程序中...

Global site tag (gtag.js) - Google Analytics