打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring文件加载方法

我知道的有三种方法:

 

      方法一,在web.xml配置文件中使用ContextLoaderListener或ContextLoaderServlet,代码如下:

<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:context/*/*.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

 或者

<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:context/*/*.xml</param-value></context-param><servlet><servlet-name>context</servlet-name><servlet-lass>org.springframework.web.context.ContextLoaderServlet</servlet-class><load-on-startup>1</load-on-startup></servlet>

 

 

  方法二,在一个主要的xml文件中(如applicationContext.xml),通过<import>标签导入其他xml文件,代码如下:

 

 <import resource="classpath:com/xml/controller*.xml"/><!--其他Bean--><bean id="userService" class="com.irp.service.UserServiceImpl"/>

 

 

 

方法三,在使用springMVC的条件下可使用,其他情况有待大家验证,在web.xml中配置DispatcherServlet的servlet标签中增加<init-param>子标签,详细代码:

 

<servlet><servlet-name>spring-mvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:context/*/*.xml</param-value></init-param><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>spring-mvc</servlet-name><url-pattern>*.htm</url-pattern></servlet-mapping>

 

使用<init-param> 子标签的好处一是简单,二可以省去spring-mvc-servlet.xml文件,如果采用第一种方法,则必须要有spring-mvc-servlet.xml文件,否则启动出现加载异常;

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Spring学习日记 - Spring的bean 配置文件路径问题
spring 使用多个XML配置文件
J2EE学习之Spring(2) 配置DispatcherServlet - 我的文章 -...
Spring中ApplicationContext加载机制和配置初始化
web.xml之context-param,listener,filter,servlet加载顺序及其周边
详解web.xml中元素的加载顺序
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服