Spring 容器的启动方式

Spring 容器的启动方式

和Struts 1.x集合时的启动方式

  • 使用struts的plugin插件启动。

在struts-config.xml文件中配置如下:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml,/WEB-INF/applicationContext.xml"/>
</plug-in>

原理:web container启动时,会执行struts 的ActionServlet的init方法。web container关闭时会执行ActionServlet的destroy方法。而struts的ActionServlet在执行init方法中会执行配置文件中注册的plug-in的init方法,同时ActionServlet的destroy方法又会调用plug-in的destroy方法。所以通过这个plug-in 可以在web应用程序启动时启动spring容器,在web应用程序关闭时又会关闭spring容器。如图:

ActionServlet的init方法:



ActionServlet的initModulePlugins方法:


ContextLoaderPlugIn的init方法:


在web.xml文件中配置,启动Spring容器

使用ContextLoaderListener

web.xml文件中配置如下:
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
这种方式会在web项目启动过程中就会启动Spring容器。

使用ContextLoaderServlet

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
这里注册Servlet时,要加上<load-on-startup>标签。否则不能在web项目启动时启动Spring容器。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值