I. overview
1.1 ask questions
- Spring MVC is a part of spring. Do you need to integrate spring MVC?
- Do you need to add Spring's IOC container?
- Do you need to configure the ContextLoadListener in the web.xml file to start the Spring IOC container?
1.2 problem solving
- Need. The configuration file of spring MVC is related to the website forwarding logic and website functions (view parser, file upload parser, ajax support...). Spring's configuration files are business related (transaction control, data sources...).
- There are also services and Dao that need to be put into the IOC container corresponding to the Spring configuration file.
- Both need and don't need, don't need: all the configuration files placed in Spring MVC, can also be divided into multiple Spring configuration files, and then use the import node to import other configuration files
2, Spring integration spring MVC experimental solution
We already know that spring and spring MVC are sub containers: Spring manages business logic components; spring MVC manages controller components;
1. Configure the listener for Spring startup and the front-end controller of Spring MVC in web.xml:
<?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 Monitor configuration --> <!-- 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 Front end controller configuration --> <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> <!-- Support Rest Style changing 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. Create the Spring configuration file: 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> <!--Configure data sources, integrate other frameworks, transactions, etc--> </beans>
3. Create the configuration file of spring MVC: 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> <!-- Configure view resolver --> <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>
Problem: if only the package scanning component is set, if there are overlaps between the IOC container of Spring and the IOC container of Spring MVC, bean s will be created twice. Solution: use the exclude filter and include filter child nodes to specify the scanned annotations.
4. Test: create BookController and BookService, add construction methods to these two classes, start the server, and view the execution of the constructor.
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..."); } }
3, The relationship between Spring IOC container and spring MVC IOC container
In the above integration, the two components of Controller and Service are respectively in different containers, but Service can be automatically injected into Controller. This shows that the beans in the IOC container of Spring MVC can reference the beans in the Spring container. What's the reverse? Not vice versa. Beans in the Spring IOC container cannot refer to beans in the Spring MVC IOC container.
- Reference the bean of the business layer in the spring MVC configuration file.
- Multiple Spring IOC containers can be set as parent-child relationship to achieve good decoupling
- The Spring MVC WEB container can be used as a sub container of the "business layer" Spring container: that is, the WEB layer container can refer to the Bean of the business layer container, while the business layer container cannot access the Bean of the WEB container.
4, Spring MVC vs. Struts
① . the entry of Spring MVC is Servlet, while Struts2 is Filter
② Spring MVC is slightly faster than struts 2. Spring MVC is based on method design, Sturts2 is based on class, and every request will instance an Action
③ . Spring MVC is simpler to use, and its development efficiency is indeed higher than struts 2: it supports JSR 303 and is more convenient to handle ajax requests
④ . the OGNL expression of struts 2 makes the page development more efficient than Spring MVC