咱們經過啓動日誌,能夠發現,spring boot 默認提供了靜態資源處理。接下來,咱們瞭解下,該如何應用默認配置,如何自定義靜態資源處理。web
INFO o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] INFO o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] INFO o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
建議你們使用Spring Boot的默認配置方式,若是須要特殊處理的再經過配置進行修改。本文主要講解Spring Boot的默認處理方式spring
上面咱們介紹了Spring Boot 的默認資源映射,通常夠用了,那咱們如何自定義目錄?segmentfault
增長 /2017imgs/ 映射到 classpath:/2017imgs/緩存
package com.wanye; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * Created by wanye on 2017/6/3. */ @Configuration public class Config extends WebMvcConfigurerAdapter{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/2017imgs/**").addResourceLocations("classpath:/2017imgs/"); } }
2017-06-15追加
看到Springboot 配置*.do請求這樣一個問題,做答以下
先說一下思路:
1.dispatcherServlet會接管全部請求(包括靜態資源請求),若是修改默認的UrlMapping爲*.do,那麼必定會致使靜態資源沒法加載。
2.仔細思考一下題主的的場景,目的但願全部*.do(擴展名)的請求,映射到controller中的method上。(對嗎?)
3.那麼問題就簡單了,參考@RequestMapping所使用的規則服務器
* <p>The mapping matches URLs using the following rules:<br> * <ul> * <li>{@code ?} matches one character</li> * <li>{@code *} matches zero or more characters</li> * <li>{@code **} matches zero or more <em>directories</em> in a path</li> * <li>{@code {spring:[a-z]+}} matches the regexp {@code [a-z]+} as a path variable named "spring"</li> * </ul>
將映射規則調整爲app
/** * Created by wanye on 2017/5/20. */ @RestController // @Controller + @ResponseBody @RequestMapping("**.do") public class HelloController { @RequestMapping(name = "hello") public Map<String, String> hello(){ Map<String, String> hello = new HashMap<String, String>(); hello.put("data", "hello 小紅"); hello.put("status", "SUCCESS"); return hello; } }
固然這只是一個例子,題主能夠將@RequestMapping("**.do")配置抽象到基類中。ide
實際應用中,咱們會有在項目服務啓動的時候就去加載一些數據或作一些事情這樣的需求。例如:白名單初始化、緩存加載、消息通知等等。爲了解決這樣的問題,Spring Boot 爲咱們提供了一個方法,只須要咱們建立類,並實現接口CommandLineRunner,固然這個類須要被spring掃描到(不要忘了增長註解)spa
package com.wanye; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /** * Created by wanye on 2017/6/3. */ @Component @Order(1) public class CacheInit implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println(">>cache init<<"); } }
當有多個啓動加載的類,能夠經過@Order來指定加載順序,按value值從小到大順序來執行。Spring Boot會將啓動參數傳給自定義啓動加載的類中run方法。debug
Spring Boot在全部內部日誌中使用Apache Commons Logging,可是默認配置也提供了對經常使用日誌的支持,如:Java Util Logging,Log4J, Log4J2和Logback。每種Logger均可以經過配置使用控制檯或者文件輸出日誌內容。本文,主要講解自定義日誌配置,並以logback爲例進行演示。日誌
因爲日誌服務通常都在ApplicationContext建立前就初始化了,它並非必須經過Spring的配置文件控制。所以經過系統屬性和傳統的Spring Boot外部配置文件依然能夠很好的支持日誌控制和管理。
根據不一樣的日誌組件,按以下規則給文件命名,就能被正確加載:
Logback:logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy Log4j:log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml Log4j2:log4j2-spring.xml, log4j2.xml JDK (Java Util Logging):logging.properties
Spring Boot官方推薦優先使用帶有-spring的文件名做爲你的日誌配置(如使用logback-spring.xml,而不是logback.xml)
<configuration debug="false" scan="true" scanPeriod="30 seconds"> <property name="FILE_PATTERN" value="%d [%t] %5p %c - %m%n"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d [%t] %5p %logger - %m%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>hello.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>hello.%d{yyyy-MM-dd}.log</FileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE"/> </root> </configuration>
控制檯輸出ConsoleAppender
文件輸出RollingFileAppender
靜態資源處理
啓動加載
日誌處理
若是以爲個人文章對您有用,請點贊、收藏。您的支持將鼓勵我繼續創做!視頻課程