Spring在web.xml中的配置参数介绍
发布时间:2023-06-17 14:34:49 所属栏目:教程 来源:
导读:在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中,但是有许多朋友们不知道Spring在web.xml中的配置到底是怎样的,现在我们去看看Spring在web.xml中的配置介绍。
在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中,但是有许多朋友们不知道Spring在web.xml中的配置到底是怎样的,现在我们去看看Spring在web.xml中的配置介绍。 目前,spring提供了两种加载器,以供web容器的加载:一种是ContextLoaderListener,另一种是ContextLoaderServlet。这两种在功能上完全相同,只是前一种是基于Servlet2.3版本中新引入的Listener接口实现,而后一种是基于Servlet接口实现,以下是这两种加载器在web.xml中的配置应用: ContextLoaderListener <listener> <listener-class>org.springframework.context.ContextLoaderListener</listener-class> </listener> ContextLoaderServlet <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> 通过上面的配置,web容器会自动加载applicationcontext.xml初始化。 如果需要指定配置文件的位置,可通过context-param加以指定: <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/myApplicationContext.xml</param-value> </context-param> 之后,可以通过WebApplicationContextUtils.getWebApplicationContext方法在web应用中获取applicationcontext的引用。 (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐