电竞比分网-中国电竞赛事及体育赛事平台

分享

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>標簽導(dǎo)入其他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文件,否則啟動出現(xiàn)加載異常;

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多