正常的整合是要在web.xml里面配置spring监听器,spring mvc的dispath servlet,代码如下:
- <!-- 监听器,用于初始化spring框架 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/config/applicationContext.xml</param-value>
- </context-param>
-
- <!-- Spring MVC 的Servlet,以启动Spring MVC模块-->
- <servlet>
- <servlet-name>dipatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/config/spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dipatcher</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
现在的是:我把一些本在spring配置文件里配置的信息,如数据源,事物,与mybatis整合的代码等等,全部移到spring-mvc.xml里面。然后在web.xml里面去掉spring监听器的代码,代码如下:
- <!-- Spring MVC 的Servlet,以启动Spring MVC模块-->
- <servlet>
- <servlet-name>dipatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/config/spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>dipatcher</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
程序一切正常,数据源,事物,与mybatis的整合都没问题
现在我想问:这两种方式的区别,哪种比较好。给个理由!