我知道的有三种方法:
方法一,在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文件,否则启动出现加载异常; |
|