在最近的项目中,使用到了spring相关的很多东西,有点把spring的配置给搞混了,从网上查到的资料以及整理了一下。

在Web项目中,启动spring容器的方式有三种,ContextLoaderListener; ContextLoaderServlet ;ContextLoaderPlugIn
  1.在web.xml中配置ContextLoaderListener,如

<context-param>

        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-context.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

可以通过<import resource ="classpath:spring/spring-xxx.xml "/>的方式把其他的配置抱进来。

2.在web.xml中配置ContextLoaderServlet,如

<servlet>

        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>

</servlet>

这种方式,spring3.0以后不再支持了

3.通过plugin配置,如

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

        <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>

</plug-in>

该方式适用于, spring与struts等整合,在Struts的配置文件struts-config.xml里面配置一个ContextLoaderPlugIn,用于spring的初始化工作。

在此建议使用用ContextLoaderListener。

此外,如果使用到了spring-mvc的话,在web.xml中配置DispatcherServlet,如下:

<servlet>

        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>     <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>

</servlet-mapping>

如果使用到了spring-security的话,在web.xml中配置FilterChain如下:

<filter>

        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>

</filter-mapping>

Spring在Web项目中的三种启动加载的配置的更多相关文章

  1. SuperDiamond在JAVA项目中的三种应用方法实践总结

    SuperDiamond在JAVA项目中的三种应用方法实践总结 1.直接读取如下: @Test public static void test_simple(){ PropertiesConfigur ...

  2. 重新学习Spring一--Spring在web项目中的启动过程

    1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...

  3. java基础-jdbc——三种方式加载驱动建立连接

    String url = "jdbc:mysql://localhost:3306/student?Unicode=true&characterEncoding=utf-8" ...

  4. 三种动态加载js的jquery实例代码另附去除js方法

    !-- 这里为你提供了三种动态加载js的jquery实例代码哦,由于jquery是为用户提供方便的,所以利用jquery动态加载文件只要一句话$.getscript("test.js&quo ...

  5. EF的三种数据加载方式

    EF的关联实体加载有三种方式:Lazy Loading,Eager Loading,Explicit Loading,其中Lazy Loading和Explicit Loading都是延迟加载. (一 ...

  6. Spring中IoC - 两种ApplicationContext加载Bean的配置

    说明:Spring IoC其实就是在Service的实现中定义了一些以来的策略类,这些策略类不是通过 初始化.Setter.工厂方法来确定的.而是通过一个叫做上下文的(ApplicationConte ...

  7. web.xml中如何设置配置文件的加载路径

    web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志.数据库.spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件 ...

  8. web.xml中servlet, bean, filter, listenr 加载顺序汇总

    最终得出结果:先 listener >> filter >> servlet >> spring 所以,如果过滤器中要使用到 bean,可以将spring 的加载 ...

  9. asp.net Web项目中使用Log4Net进行错误日志记录

      使用log4net可以很方便地为应用添加日志功能.应用Log4net,开发者可以很精确地控制日志信息的输出,减少了多余信息,提高了日志记录性能.同时,通过外部配置文件,用户可以不用重新编译程序就能 ...

随机推荐

  1. Sharepoint中用treeview来显示组织机构的人员状态的webpart

    转:http://www.cnblogs.com/virusswb/archive/2009/04/28/1445517.html

  2. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.6

    Let $A$ be a nilpotent operator. Show how to obtain, from aJordan basis for $A$, aJordan basis of $\ ...

  3. Anti-Grain Geometry 概述

    AGG是一个轻量.灵活.可靠的图形算法库,AGG各部分之间是松耦合的,也即是说各部分可以单独使用. The primary goal of Anti-Grain Geometry is to brea ...

  4. cygwin远程操作linux

    远程登录 1.ssh <username>@<IP> eg:ssh root@10.20.30.255 2.输入密码就OK 远程拷贝 1.scp -r <username ...

  5. 题目1043:Day of Week(输入日期与当前日起天数差%7,在做相关星期调整)

    题目描述: We now use the Gregorian style of dating in Russia. The leap years are years with number divis ...

  6. Android Activity 生命周期中onStart()和onResume()的区别

    首先了解Activity的四种状态 Running状态:一个新的Activity启动入栈后,它在屏幕最前端,处于栈的最顶端,此时它处于可见并可和用户交互的激活状态.Paused状态:当Activity ...

  7. MySQL在线备份与恢复工具 --> Xtrabackup

    1 Xtrabackup原理简介 xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品.  ...

  8. MapReduce之Partition的使用与分析

    Partition主要作用就是将map的结果发送到相应的reduce.这就对partition有两个要求: 1)均衡负载,尽量的将工作均匀的分配给不同的reduce. 2)效率,分配速度一定要快. M ...

  9. 题解西电OJ (Problem 1008 - 数星星)

    题目内容: Description “不要问我太阳有多高 我会告诉你我有多真 不要问我星星有几颗 我会告诉你很多很多” 一天Qinz和wudired在天上数星星,由于星星可以排列成一条直线,他们比赛看 ...

  10. leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

    https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...