一.概述
1.1 提出问题
- SpringMVC是Spring中的一部分,那么需要进行Spring整合SpringMVC?
- 还是否需要在加入Spring的IOC容器?
- 是否需要在web.xml文件中配置启动Spring IOC容器的ContextLoadListener?
1.2 解决问题
- 需要。SpringMVC的配置文件就来配置和网站转发逻辑以及网站功能有关(视图解析器,文件上传解析器,支持ajax...)。Spring的配置文件和业务有关的(事务控制,数据源...)。
- 需要 放入Spring配置文件对应的IOC容器中还有Service和Dao。
- 既需要又不需要,不需要:都放在SpringMVC的配置文件,也可以分为多个Spring的配置文件,然后使用import节点导入其他的配置文件
二.Spring整合SpringMVC实验解决方法
我们已经知道Spring和SpringMVC分容器:Spring管理业务逻辑组件;SpringMVC管理控制器组件;
1.在web.xml中配置Spring启动时用的监听器和SpringMVC的前端控制器:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>6.Spring_crud</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<!-- Spring监听器配置 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- SpringMVC前端控制器配置 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 支持Rest风格转换的filter -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.创建Spring的配置文件:spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.test">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置数据源,整合其他框架,事务等-->
</beans>
3.创建SpringMVC的配置文件:springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.test" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResouceViewResolver">
<property name="suffix" value="/WEB-INF/pages/"></property>
<property name="prefix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
问题:若只单单设定包扫描组件,若Spring的IOC容器和SpringMVC的IOC容器扫描的包有重合的部分,就会导致有bean被创建2次。解决方法:使用exclude-filter和include-filter子节点来规定扫描的注解。
4.测试:创建BookController和BookService,在这两个类中增加构造方法,启动服务器,查看构造器执行情况。
package com.test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.test.service.BookService;
@Controller
public class BookController {
@Autowired
private BookService bookService;
public BookController()
{
System.out.println("BookController...");
}
@RequestMapping("/hello")
public String hello(){
System.out.println(bookService);
return "forward:/index.jsp";
}
}
package com.test.service;
import org.springframework.stereotype.Service;
@Service
public class BookService {
public BookService(){
System.out.println("BookService...");
}
}
三.Spring IOC容器和SpringMVC IOC容器的关系
在上面整合中,Controller和Service两个组件分别在不同的容器中,但是可以在Controller中自动注入Service。这就说明:SpringMVC的IOC容器中的bean可以来引用Spring容器中的bean。反过来呢?反之则不行。Spring IOC容器中的bean却不能引用Spring MVC IOC容器中的bean。
- 在SpringMVC配置文件中引用业务层的bean。
- 多个Spring IOC容器中间可以设置为父子关系,以实现良好的解耦
- Spring MVC WEB容器可作为“业务层”Spring 容器的子容器:即WEB层容器可以引用业务层容器的Bean,而业务层容器却访问不到WEB容器的Bean。
四.SpringMVC对比Struts
①. Spring MVC 的入口是 Servlet, 而 Struts2 是 Filter
②. Spring MVC 会稍微比 Struts2 快些. Spring MVC 是基于方法设计, 而 Sturts2 是基于类, 每次发一次请求都会实例一个 Action.
③. Spring MVC 使用更加简洁, 开发效率Spring MVC确实比 struts2 高: 支持 JSR303, 处理 ajax 的请求更方便
④. Struts2 的 OGNL 表达式使页面的开发效率相比 Spring MVC 更高些