分享

Spring文件加载方法

 昵称5747462 2011-02-15

我知道的有三种方法:

 

      方法一,在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文件,否则启动出现加载异常;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多