web.xml 记载spring 配置文件的写法
2017-03-24 17:19
411 查看
在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制,自动加载的容器中去,在web项目中,配置文件加载到web容器中进行解析,目前,spring提供了两种加载器,以供web容器的加载:一种是ContextLoaderListener,另一种是ContextLoaderServlet。这两种在功能上完全相同,只是一种是基于Servlet2.3版本中新引入的Listener接口实现,而另一种是基于Servlet接口实现,以下是这两种加载器在web.xml中的时机配置应用:
第一种:
<listener>
<listener-class>org.springframework.context.ContextLoaderListener</listener-class>
</listener>
另一种:
<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的引用。
第一种:
<listener>
<listener-class>org.springframework.context.ContextLoaderListener</listener-class>
</listener>
另一种:
<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的引用。
相关文章推荐
- spring多个配置文件web.xml写法
- 多个spring配置文件时在web.xml中的写法
- 基于注解的Struts2+Spring2.5+Hibernate3.3的Spring 配置文件,struts配置文件,web.xml配置文件的例子
- spring的配置文件在web.xml中加载的方式
- spring配置文件路径修改后web.xml配置
- spring配置文件路径修改后web.xml配置
- Spring配置web.xml文件时遇到的问题
- 在Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别
- Spring配置文件 在web.xml的几种配置方式
- Spring常用的web.xml文件配置
- JavaEE 下面使用Spring 时 web.xml 中ContextLoaderListener的作用及配置,多个配置文件时contextConfigLocation的作用
- spring+hibernate applicationContext.xml配置文件写法
- spring配置文件的一般写法(集成hibernate) (applicationContext.xml)
- [spring]Spring MVC web.xml 配置文件配置
- 在web.xml中配置spring配置文件的路径
- jsf、Spring、Hibernate 集成 web.xml配置文件
- web.xml集成Spring,Struts配置文件说明
- web.xml 文件中一般包括 servlet, spring, filter, listenr的配置。那么他们是按照一个什么顺序加载呢?
- web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序 研究
- 基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填参,EL取值)