SpringMVC是比較常用的JavaWeb框架,非常輕便強悍,能簡化Web開發,大大提高開發效率,在各種Web程序中廣泛應用。本文采用Java Config的方式搭建SpringMVC項目,並對SpringMVC啟動時加載順序做簡單的說明。
1、SpringMVC啟動流程圖
2、SpringMVC項目啟動流程介紹
SpringMVC 是Spring 框架的重要模塊,借助於Spring 的容器技術,可以非常方面的搭建Web項目。
SpringMVC項目啟動時要完成Spring 容器的初始化和SpringMVC配置的初始化。
2.1 Spring容器的初始化:
1、項目中需要配置ContextLoadListener監聽器,它會監聽項目的啟動,在項目啟動時調用容器初始化方法完成Spring容器的初始化
如果采用XML配置,通常需要在web.xml文件里面添加如下配置:
本文采用Java Config 實現,所以在配置中繼承了AbstractAnnotationConfigDispatcherServletInitializer 這個抽象類,這個類的繼承關系如下:
在父類AbstractContextLoaderInitializer中注冊了ContextLoadListener監聽器:
具體如下:
2、在需要在容器初始化時創建的類上面加上注解,就可以實現Bean的自動裝配了。
@Controller @Service @Bean @Conponent等注解都可以顯示表明Bean需要自動裝配
3、配置Bean初始化的范圍
2.2 SpringMVC 的配置
1、配置SpringMVC需要添加DispatchServlet ,DispatcherServlet主要負責前端調用URL的分發,他在Web容器初始化的時候被注冊。在2.1中,我們已經知道,本文配置中繼承了DispatchServlet 的一個抽象類AbstractAnnotationConfigDispatcherServletInitializer ,此抽象類的父類在實例化的時候會注冊一個DispatchServlet到容器中,方法名如下。
2、定義視圖解析器
前端訪問URL,DispatchServlet 會把URL 匹配到Controller中相應的@RequstMapper的方法上去,該方法處理完請求后返回需要的業務數據模型,然后會調用自己的視圖解析器把數據渲染到前端頁面中,渲染之后返回給瀏覽器。
視圖解析器定義如下:
3、其實SpringMVC 項目啟動時配置的東西還有很多,HandlerException 異常處理,數據校驗等,這些Spring提供的抽象類WebMvcConfigurerAdapter中已經實現好了,我們在項目中直接繼承就行了。
3、代碼實現:
項目采用Maven管理:pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.beiyan</groupId> <artifactId>demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>demo Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <!-- Test --> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- SpringMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.3.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
項目結構如下:
RootConfig類中配置了包掃描的范圍:
package com.beiyan.demo.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages = { "com.beiyan.demo" }) public class RootConfig { }
WebConfig 中配置了視圖解析器以及RequestMapper的掃描范圍
package com.beiyan.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc @ComponentScan("com.beiyan.demo.controller") public class WebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } /** * 啟用spring mvc 的注解 */ public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
WebAppInitializer類配置了容器初始化時需要加載的配置類
package com.beiyan.demo.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { RootConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { WebConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } }
TestController中定義了測試用的URL:
package com.beiyan.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { @RequestMapping(value = "/test", method = RequestMethod.GET) public String test() { return "test"; } }
項目的Web 文件如下:
啟動項目后,瀏覽器訪問http://localhost:8080/SpringServlet/test 可以成功訪問如下:
至此,SpringMVC項目配置成功了。
寫在后面的話:
本項目只是簡單搭建了SpringMVC項目,采用了最簡配置,實際項目中可能會對Servlet,Filter進行自定義,還會添加Mybatis /hibernate, SpringSecurity等功能。關於JPA,SpringSecurity,Themeleaf 的集成將會在下一篇關於SpringBoot 的項目搭建中集成。
本項目完整代碼已上傳開源中國Git倉庫:https://git.oschina.net/beiyan/mvc-config