Spring在Web项目中的三种启动加载的配置

在最近的项目中,使用到了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> 

 

 

转载于:https://www.cnblogs.com/kingcucumber/p/3142183.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值