整合springMVC
4.3.整合SpringMVC
雖然默認配置已經可以使用SpringMVC了,不過我們有時候需要進行自定義配置。
4.3.1.修改端口
添加全局配置文件:application.properties
端口通過以下方式配置
# 映射端口 server.port=804.3.2.訪問靜態資源
現在,我們的項目是一個jar工程,那么就沒有webapp,我們的靜態資源該放哪里呢?
回顧我們上面看的源碼,有一個叫做ResourceProperties的類,里面就定義了靜態資源的默認查找路徑:
默認的靜態資源路徑為:
-
classpath:/META-INF/resources/
-
classpath:/resources/
-
classpath:/static/
-
classpath:/public/
只要靜態資源放在這些目錄中任何一個,SpringMVC都會幫我們處理。
我們習慣會把靜態資源放在classpath:/static/目錄下。我們創建目錄,并且添加一些靜態資源:
4.3.3.添加攔截器
攔截器也是我們經常需要使用的,在SpringBoot中該如何配置呢?
攔截器不是一個普通屬性,而是一個類,所以就要用到java配置方式了。在SpringBoot官方文檔中有這么一段說明:
If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.
翻譯:
如果你想要保持Spring Boot 的一些默認MVC特征,同時又想自定義一些MVC配置(包括:攔截器,格式化器, 視圖控制器、消息轉換器 等等),你應該讓一個類實現WebMvcConfigurer,并且添加@Configuration注解,但是千萬不要加@EnableWebMvc注解。如果你想要自定義HandlerMapping、HandlerAdapter、ExceptionResolver等組件,你可以創建一個WebMvcRegistrationsAdapter實例 來提供以上組件。
如果你想要完全自定義SpringMVC,不保留SpringBoot提供的一切特征,你可以自己定義類并且添加@Configuration注解和@EnableWebMvc注解
?
總結:通過實現WebMvcConfigurer并添加@Configuration注解來實現自定義部分SpringMvc配置。
實現如下:
首先我們定義一個攔截器:
package com.learn.user.interceptor;import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;@Component public class MyInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle method is running!");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle method is running!");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion method is running!");} }然后定義配置類,注冊攔截器:
package com.learn.user.config;import com.learn.user.interceptor.MyInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration public class MvcConfiguration implements WebMvcConfigurer {@Autowiredprivate MyInterceptor myInterceptor;/*** 重寫接口中的addInterceptors方法,添加自定義攔截器* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(myInterceptor).addPathPatterns("/**");} }接下來運行并查看日志:
preHandle method is running! postHandle method is running! afterCompletion method is running!你會發現日志中只有這些打印信息,springMVC的日志信息都沒有,因為springMVC記錄的log級別是debug,springboot默認是顯示info以上,我們需要進行配置。
SpringBoot通過logging.level.*=debug來配置日志級別,*填寫包名
# 設置org.springframework包的日志級別為debug logging.level.org.springframework=debug?
總結
以上是生活随笔為你收集整理的整合springMVC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot的自动配置原理
- 下一篇: MVC 顶层设计-HandlerMapp