SpringBoot 學習筆記(一)
阿新 • • 發佈:2018-12-13
1.springboot內嵌tomcat,但是不支援jsp,如果需要,就手動匯入jsp,以下是相關配置
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
2.springboot支援自動配置,可以手動關閉某一項的自動配置
例:@SpringBootApplication(exclude={RedisAutoConfiguration.class})
3.Springboot的配置檔案application.properties或者 application.yml,
4.springboot配置springmvc攔截器
a .在springboot主入口程式包下新建一個類繼承WebMvcConfigurerAdapter,
b.類上面加註解@Configuration
c.重新寫HandlerInerceptor
import java.nio.charset.Charset; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration //申明這是一個配置 public class MySrpingMVCConfig extends WebMvcConfigurerAdapter{ // 自定義攔截器 @Override public void addInterceptors(InterceptorRegistry registry) { HandlerInterceptor handlerInterceptor = new HandlerInterceptor() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("自定義攔截器............"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }; registry.addInterceptor(handlerInterceptor).addPathPatterns("/**"); } }
執行流程:Springboot主入口程式的 @ComponentScan 註解會掃描主入口程式包下當前包及子包下所有類。
掃描到我們定義的類 後有註解configuration 就會繼續掃描,執行攔截器
5.SpringBoot專案釋出到獨立的Tomcat中執行
a.工程的打包方式 為war包
b.將Spring-boot-starter-tomcat的範圍設定為provided。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
c.修改程式碼,設定啟動配置
需要繼承SpringBootServletInitializer,然後重寫configure,將SpringBoot的入口類設定進去
d.打包。