乐优商城笔记
如需資料請加q群572827051
0.學習目標
- 了解SpringBoot的作用
- 掌握java配置的方式
- 了解SpringBoot自動配置原理
- 掌握SpringBoot的基本使用
- 了解Thymeleaf的基本使用
1. 了解SpringBoot
在這一部分,我們主要了解以下3個問題:
- 什么是SpringBoot
- 為什么要學習SpringBoot
- SpringBoot的特點
1.1.什么是SpringBoot
SpringBoot是Spring項目中的一個子工程,與我們所熟知的Spring-framework 同屬于spring的產品:
我們可以看到下面的一段介紹:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
翻譯一下:
Spring Boot你只需要“run”就可以非常輕易的構建獨立的、生產級別的spring應用。
我們為spring平臺和第三方依賴庫提供了一種固定化的使用方式,使你能非常輕松的開始開發你的應用程序。大部分Spring Boot應用只需要很少的配置。
其實人們把Spring Boot稱為搭建程序的腳手架。其最主要作用就是幫我們快速的構建龐大的spring項目,并且盡可能的減少一切xml配置,做到開箱即用,迅速上手,讓我們關注于業務而非配置。
我們可以使用SpringBoot創建java應用,并使用java –jar 啟動它,就能得到一個生產級別的web工程。
1.2.為什么要學習SpringBoot
java一直被人詬病的一點就是臃腫、麻煩。當我們還在辛苦的搭建項目時,可能Python程序員已經把功能寫好了,究其原因主要是兩點:
-
復雜的配置
項目各種配置其實是開發時的損耗, 因為在思考 Spring 特性配置和解決業務問題之間需要進行思維切換,所以寫配置擠占了寫應用程序邏輯的時間。
-
混亂的依賴管理
項目的依賴管理也是件吃力不討好的事情。決定項目里要用哪些庫就已經夠讓人頭痛的了,你還要知道這些庫的哪個版本和其他庫不會有沖突,這也是件棘手的問題。并且,依賴管理也是一種損耗,添加依賴不是寫應用程序代碼。一旦選錯了依賴的版本,隨之而來的不兼容問題毫無疑問會是生產力殺手。
而SpringBoot讓這一切成為過去!
1.3.SpringBoot的特點
Spring Boot 主要特征是:
- 創建獨立的spring應用程序
- 直接內嵌tomcat、jetty和undertow(不需要打包成war包部署)
- 提供了固定化的“starter”配置,以簡化構建配置
- 盡可能的自動配置spring和第三方庫
- 提供產品級的功能,如:安全指標、運行狀況監測和外部化配置等
- 絕對不會生成代碼,并且不需要XML配置
總之,Spring Boot為所有 Spring 的開發者提供一個開箱即用的、非??焖俚?、廣泛接受的入門體驗
更多細節,大家可以到官網查看。
2.快速入門
接下來,我們就來利用SpringBoot搭建一個web工程,體會一下SpringBoot的魅力所在!
環境要求:
2.1.創建工程
我們先新建一個空的demo工程,如下:
創建以moduel:
填寫坐標信息:
目錄結構:
創建完成后的目錄結構:
2.2.引入依賴
看到這里很多同學會有疑惑,前面說傳統開發的問題之一就是依賴管理混亂,怎么這里我們還需要管理依賴呢?難道SpringBoot不幫我們管理嗎?
別著急,現在我們的項目與SpringBoot還沒有什么關聯。SpringBoot提供了一個名為spring-boot-starter-parent的工程,里面已經對各種常用依賴(并非全部)的版本進行了管理,我們的項目需要以這個項目為父工程,這樣我們就不用操心依賴的版本問題了,需要什么依賴,直接引入坐標即可!
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.itcast.springboot</groupId><artifactId>itcast-springboot</artifactId><version>1.0-SNAPSHOT</version><!-- 所有的springboot的工程都以spring父工程為父工程 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.6.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies> </project>2.3.編寫HelloController
代碼:
@RestController @EnableAutoConfiguration public class HelloController {@GetMapping("show")public String test(){return "hello Spring Boot!";}public static void main(String[] args) {SpringApplication.run(HelloController.class, args);} }2.4.啟動測試
bingo!訪問成功!
2.5.詳解
入門工程中:pom.xml里引入了啟動器的概念以@EnableAutoConfiguration注解。
2.5.1.啟動器
為了讓SpringBoot幫我們完成各種自動配置,我們必須引入SpringBoot提供的自動配置依賴,我們稱為啟動器。spring-boot-starter-parent工程將依賴關系聲明為一個或者多個啟動器,我們可以根據項目需求引入相應的啟動器,因為我們是web項目,這里我們引入web啟動器:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> </dependencies>需要注意的是,我們并沒有在這里指定版本信息。因為SpringBoot的父工程已經對版本進行了管理了。
這個時候,我們會發現項目中多出了大量的依賴:
這些都是SpringBoot根據spring-boot-starter-web這個依賴自動引入的,而且所有的版本都已經管理好,不會出現沖突。
2.5.2.@EnableAutoConfiguration
關于這個注解,官網上有一段說明:
Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined.
簡單翻譯以下:
開啟spring應用程序的自動配置,SpringBoot基于你所添加的依賴和你自己定義的bean,試圖去猜測并配置你想要的配置。比如我們引入了spring-boot-starter-web,而這個啟動器中幫我們添加了tomcat、SpringMVC的依賴。此時自動配置就知道你是要開發一個web應用,所以就幫你完成了web及SpringMVC的默認配置了!
總結,SpringBoot內部對大量的第三方庫或Spring內部庫進行了默認配置,這些配置是否生效,取決于我們是否引入了對應庫所需的依賴,如果有那么默認配置就會生效。
所以,我們使用SpringBoot構建一個項目,只需要引入所需依賴,配置就可以交給SpringBoot處理了。
2.6.優化入門程序
現在工程中只有一個Controller,可以這么玩;那么如果有多個Controller,怎么辦呢?
添加Hello2Controller:
代碼:
@RestController public class Hello2Controller {@GetMapping("show2")public String test(){return "hello Spring Boot2!";}}啟動重新啟動,訪問show2測試,失敗:
難道要在每一個Controller中都添加一個main方法和@EnableAutoConfiguration注解,這樣啟動一個springboot程序也太麻煩了。也無法同時啟動多個Controller,因為每個main方法都監聽8080端口。所以,一個springboot程序應該只有一個springboot的main方法。
所以,springboot程序引入了一個全局的引導類。
2.5.1.添加引導類
通常請求下,我們在一個springboot工程中都會在基包下創建一個引導類,一些springboot的全局注解(@EnableAutoConfiguration注解)以及springboot程序的入口main方法都放在該類中。
在springboot的程序的基包下(引導類和Controller包在同級目錄下),創建TestApplication.class:
內容如下:
@EnableAutoConfiguration public class TestApplication {public static void main(String[] args) {SpringApplication.run(TestApplication.class, args);} }并修改HelloController,去掉main方法及@EnableAutoConfiguration:
@RestController public class HelloController {@GetMapping("show")public String test(){return "hello Spring Boot!";} }啟動引導類,訪問show測試:
發現所有的Controller都不能訪問,為什么?
回想以前程序,我們在配置文件中添加了注解掃描,它能掃描指定包下的所有Controller,而現在并沒有。怎么解決——@ComponentScan注解
2.5.2.@ComponentScan
spring框架除了提供配置方式的注解掃描<context:component-scan />,還提供了注解方式的注解掃描@ComponentScan。
在TestApplication.class中,使用@ComponentScan注解:
@EnableAutoConfiguration @ComponentScan public class TestApplication {public static void main(String[] args) {SpringApplication.run(TestApplication.class, args);}}重新啟動,訪問show或者show2:
我們跟進該注解的源碼,并沒有看到什么特殊的地方。我們查看注釋:
大概的意思:
配置組件掃描的指令。提供了類似與<context:component-scan>標簽的作用
通過basePackageClasses或者basePackages屬性來指定要掃描的包。如果沒有指定這些屬性,那么將從聲明這個注解的類所在的包開始,掃描包及子包
而我們的@ComponentScan注解聲明的類就是main函數所在的啟動類,因此掃描的包是該類所在包及其子包。一般啟動類會放在一個比較淺的包目錄中。
2.5.3.@SpringBootApplication
我們現在的引導類中使用了@EnableAutoConfiguration和@ComponentScan注解,有點麻煩。springboot提供了一種簡便的玩法:@SpringBootApplication注解
使用@SpringBootApplication改造TestApplication:
@SpringBootApplication public class TestApplication {public static void main(String[] args) {SpringApplication.run(TestApplication.class, args);}}點擊進入,查看源碼:
發現@SpringBootApplication其實是一個組合注解,這里重點的注解有3個:
- @SpringBootConfiguration
- @EnableAutoConfiguration:開啟自動配置
- @ComponentScan:開啟注解掃描
2.5.4.@SpringBootConfiguration
@SpringBootConfiguration注解的源碼:
我們繼續點擊查看源碼:
通過這段我們可以看出,在這個注解上面,又有一個@Configuration注解。通過上面的注釋閱讀我們知道:這個注解的作用就是聲明當前類是一個配置類,然后Spring會自動掃描到添加了@Configuration的類,并且讀取其中的配置信息。而@SpringBootConfiguration是來聲明當前類是SpringBoot應用的配置類,項目中只能有一個。所以一般我們無需自己添加。
3.默認配置原理
springboot的默認配置方式和我們之前玩的配置方式不太一樣,沒有任何的xml。那么如果自己要新增配置該怎么辦?比如我們要配置一個數據庫連接池,以前會這么玩:
<!-- 配置連接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /> </bean>現在該怎么做呢?
3.1.回顧歷史
事實上,在Spring3.0開始,Spring官方就已經開始推薦使用java配置來代替傳統的xml配置了,我們不妨來回顧一下Spring的歷史:
-
Spring1.0時代
在此時因為jdk1.5剛剛出來,注解開發并未盛行,因此一切Spring配置都是xml格式,想象一下所有的bean都用xml配置,細思極恐啊,心疼那個時候的程序員2秒
-
Spring2.0時代
Spring引入了注解開發,但是因為并不完善,因此并未完全替代xml,此時的程序員往往是把xml與注解進行結合,貌似我們之前都是這種方式。
-
Spring3.0及以后
3.0以后Spring的注解已經非常完善了,因此Spring推薦大家使用完全的java配置來代替以前的xml,不過似乎在國內并未推廣盛行。然后當SpringBoot來臨,人們才慢慢認識到java配置的優雅。
有句古話說的好:擁抱變化,擁抱未來。所以我們也應該順應時代潮流,做時尚的弄潮兒,一起來學習下java配置的玩法。
3.2.嘗試java配置
java配置主要靠java類和一些注解來達到和xml配置一樣的效果,比較常用的注解有:
- @Configuration:聲明一個類作為配置類,代替xml文件
- @Bean:聲明在方法上,將方法的返回值加入Bean容器,代替<bean>標簽
- @Value:屬性注入
- @PropertySource:指定外部屬性文件。
我們接下來用java配置來嘗試實現連接池配置
3.2.1.引入依賴
首先在pom.xml中,引入Druid連接池依賴:
<dependency><groupId>com.github.drtrang</groupId><artifactId>druid-spring-boot2-starter</artifactId><version>1.1.10</version> </dependency>3.2.2.添加jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/leyou jdbc.username=root jdbc.password=1233.2.3.配置數據源
創建JdbcConfiguration類:
@Configuration @PropertySource("classpath:jdbc.properties") public class JdbcConfiguration {@Value("${jdbc.url}")String url;@Value("${jdbc.driverClassName}")String driverClassName;@Value("${jdbc.username}")String username;@Value("${jdbc.password}")String password;@Beanpublic DataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setUrl(url);dataSource.setDriverClassName(driverClassName);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;} }解讀:
- @Configuration:聲明JdbcConfiguration是一個配置類。
- @PropertySource:指定屬性文件的路徑是:classpath:jdbc.properties
- 通過@Value為屬性注入值。
- 通過@Bean將 dataSource()方法聲明為一個注冊Bean的方法,Spring會自動調用該方法,將方法的返回值加入Spring容器中。相當于以前的bean標簽
然后就可以在任意位置通過@Autowired注入DataSource了!
3.2.4.測試
我們在HelloController中測試:
@RestController public class HelloController {@Autowiredprivate DataSource dataSource;@GetMapping("show")public String test(){return "hello Spring Boot!";}}在test方法中打一個斷點,然后Debug運行并查看:
屬性注入成功了!
3.3.SpringBoot的屬性注入
在上面的案例中,我們實驗了java配置方式。不過屬性注入使用的是@Value注解。這種方式雖然可行,但是不夠強大,因為它只能注入基本類型值。
在SpringBoot中,提供了一種新的屬性注入方式,支持各種java基本數據類型及復雜類型的注入。
1)新建JdbcProperties,用來進行屬性注入:
代碼:
@ConfigurationProperties(prefix = "jdbc") public class JdbcProperties {private String url;private String driverClassName;private String username;private String password;// ... 略// getters 和 setters }- 在類上通過@ConfigurationProperties注解聲明當前類為屬性讀取類
- prefix="jdbc"讀取屬性文件中,前綴為jdbc的值。
- 在類上定義各個屬性,名稱必須與屬性文件中jdbc.后面部分一致,并且必須具有getter和setter方法
- 需要注意的是,這里我們并沒有指定屬性文件的地址,SpringBoot默認會讀取文件名為application.properties的資源文件,所以我們把jdbc.properties名稱改為application.properties
2)在JdbcConfiguration中使用這個屬性:
-
通過@EnableConfigurationProperties(JdbcProperties.class)來聲明要使用JdbcProperties這個類的對象
-
然后你可以通過以下方式在JdbcConfiguration類中注入JdbcProperties:
- @Autowired注入 @Configuration @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfiguration {@Autowiredprivate JdbcProperties jdbcProperties;@Beanpublic DataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setUrl(jdbcProperties.getUrl());dataSource.setDriverClassName(jdbcProperties.getDriverClassName());dataSource.setUsername(jdbcProperties.getUsername());dataSource.setPassword(jdbcProperties.getPassword());return dataSource;}}
- 構造函數注入 @Configuration @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfiguration {private JdbcProperties jdbcProperties;public JdbcConfiguration(JdbcProperties jdbcProperties){this.jdbcProperties = jdbcProperties;}@Beanpublic DataSource dataSource() {// 略}}
- @Bean方法的參數注入 @Configuration @EnableConfigurationProperties(JdbcProperties.class) public class JdbcConfiguration {@Beanpublic DataSource dataSource(JdbcProperties jdbcProperties) {// ...} }
本例中,我們采用第三種方式。
3)測試結果:
大家會覺得這種方式似乎更麻煩了,事實上這種方式有更強大的功能,也是SpringBoot推薦的注入方式。兩者對比關系:
優勢:
- Relaxed binding:松散綁定
- 不嚴格要求屬性文件中的屬性名與成員變量名一致。支持駝峰,中劃線,下劃線等等轉換,甚至支持對象引導。比如:user.friend.name:代表的是user對象中的friend屬性中的name屬性,顯然friend也是對象。@value注解就難以完成這樣的注入方式。
- meta-data support:元數據支持,幫助IDE生成屬性提示(寫開源框架會用到)。
3.4.更優雅的注入
事實上,如果一段屬性只有一個Bean需要使用,我們無需將其注入到一個類(JdbcProperties)中。而是直接在需要的地方聲明即可:
@Configuration public class JdbcConfiguration {@Bean// 聲明要注入的屬性前綴,SpringBoot會自動把相關屬性通過set方法注入到DataSource中@ConfigurationProperties(prefix = "jdbc")public DataSource dataSource() {DruidDataSource dataSource = new DruidDataSource();return dataSource;} }我們直接把@ConfigurationProperties(prefix = "jdbc")聲明在需要使用的@Bean的方法上,然后SpringBoot就會自動調用這個Bean(此處是DataSource)的set方法,然后完成注入。使用的前提是:該類必須有對應屬性的set方法!
我們將jdbc的url改成:/heima,再次測試:
3.5.SpringBoot中的默認配置
通過剛才的學習,我們知道@EnableAutoConfiguration會開啟SpringBoot的自動配置,并且根據你引入的依賴來生效對應的默認配置。那么問題來了:
- 這些默認配置是怎么配置的,在哪里配置的呢?
- 為何依賴引入就會觸發配置呢?
- 這些默認配置的屬性來自哪里呢?
其實在我們的項目中,已經引入了一個依賴:spring-boot-autoconfigure,其中定義了大量自動配置類:
還有:
非常多,幾乎涵蓋了現在主流的開源框架,例如:
- redis
- jms
- amqp
- jdbc
- jackson
- mongodb
- jpa
- solr
- elasticsearch
… 等等
我們來看一個我們熟悉的,例如SpringMVC,查看mvc 的自動配置類:
打開WebMvcAutoConfiguration:
我們看到這個類上的4個注解:
-
@Configuration:聲明這個類是一個配置類
-
@ConditionalOnWebApplication(type = Type.SERVLET)
ConditionalOn,翻譯就是在某個條件下,此處就是滿足項目的類是是Type.SERVLET類型,也就是一個普通web工程,顯然我們就是
-
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
這里的條件是OnClass,也就是滿足以下類存在:Servlet、DispatcherServlet、WebMvcConfigurer,其中Servlet只要引入了tomcat依賴自然會有,后兩個需要引入SpringMVC才會有。這里就是判斷你是否引入了相關依賴,引入依賴后該條件成立,當前類的配置才會生效!
-
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
這個條件與上面不同,OnMissingBean,是說環境中沒有指定的Bean這個才生效。其實這就是自定義配置的入口,也就是說,如果我們自己配置了一個WebMVCConfigurationSupport的類,那么這個默認配置就會失效!
接著,我們查看該類中定義了什么:
視圖解析器:
處理器適配器(HandlerAdapter):
還有很多,這里就不一一截圖了。
另外,這些默認配置的屬性來自哪里呢?
我們看到,這里通過@EnableAutoConfiguration注解引入了兩個屬性:WebMvcProperties和ResourceProperties。
我們查看這兩個屬性類:
找到了內部資源視圖解析器的prefix和suffix屬性。
ResourceProperties中主要定義了靜態資源(.js,.html,.css等)的路徑:
如果我們要覆蓋這些默認屬性,只需要在application.properties中定義與其前綴prefix和字段名一致的屬性即可。
3.6.總結
SpringBoot為我們提供了默認配置,而默認配置生效的條件一般有兩個:
- 你引入了相關依賴
- 你自己沒有配置
1)啟動器
之所以,我們如果不想配置,只需要引入依賴即可,而依賴版本我們也不用操心,因為只要引入了SpringBoot提供的stater(啟動器),就會自動管理依賴及版本了。
因此,玩SpringBoot的第一件事情,就是找啟動器,SpringBoot提供了大量的默認啟動器,參考課前資料中提供的《SpringBoot啟動器.txt》
2)全局配置
另外,SpringBoot的默認配置,都會讀取默認屬性,而這些屬性可以通過自定義application.properties文件來進行覆蓋。這樣雖然使用的還是默認配置,但是配置中的值改成了我們自定義的。
因此,玩SpringBoot的第二件事情,就是通過application.properties來覆蓋默認屬性值,形成自定義配置。我們需要知道SpringBoot的默認屬性key,非常多,參考課前資料提供的:《SpringBoot全局屬性.md》
4.SpringBoot實戰
接下來,我們來看看如何用SpringBoot來玩轉以前的SSM,我們沿用之前講解SSM用到的數據庫tb_user和實體類User
4.1.創建工程
4.2.編寫基本代碼
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.itcast.user</groupId><artifactId>itcast-user</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.6.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>參照上邊的項目,編寫引導類:
@SpringBootApplication public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class);} }編寫UserController:
@RestController @RequestMapping("user") public class UserController {@GetMapping("hello")public String test(){return "hello ssm";} }4.3.整合SpringMVC
雖然默認配置已經可以使用SpringMVC了,不過我們有時候需要進行自定義配置。
4.3.1.修改端口
添加全局配置文件:application.properties
端口通過以下方式配置
# 映射端口 server.port=80重啟服務后測試:
4.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配置。
實現如下:
首先我們定義一個攔截器:
@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!");} }然后定義配置類,注冊攔截器:
@Configuration public class MvcConfiguration implements WebMvcConfigurer {@Autowiredprivate HandlerInterceptor 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再次運行查看:
4.4.整合連接池
jdbc連接池是spring配置中的重要一環,在SpringBoot中該如何處理呢?
答案是不需要處理,我們只要找到SpringBoot提供的啟動器即可:
在pom.xml中引入jdbc的啟動器:
<!--jdbc的啟動器,默認使用HikariCP連接池--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--不要忘記數據庫驅動,因為springboot不知道我們使用的什么數據庫,這里選擇mysql--> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId> </dependency>SpringBoot已經自動幫我們引入了一個連接池:
HikariCP應該是目前速度最快的連接池了,我們看看它與c3p0的對比:
因此,我們只需要指定連接池參數即可:
# 連接四大參數 spring.datasource.url=jdbc:mysql://localhost:3306/heima spring.datasource.username=root spring.datasource.password=root # 可省略,SpringBoot自動推斷 spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.hikari.idle-timeout=60000 spring.datasource.hikari.maximum-pool-size=30 spring.datasource.hikari.minimum-idle=10當然,如果你更喜歡Druid連接池,也可以使用Druid官方提供的啟動器:
<!-- Druid連接池 --> <dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.6</version> </dependency>而連接信息的配置與上面是類似的,只不過在連接池特有屬性上,方式略有不同:
#初始化連接數 spring.datasource.druid.initial-size=1 #最小空閑連接 spring.datasource.druid.min-idle=1 #最大活動連接 spring.datasource.druid.max-active=20 #獲取連接時測試是否可用 spring.datasource.druid.test-on-borrow=true #監控頁面啟動 spring.datasource.druid.stat-view-servlet.allow=true4.5.整合mybatis
4.5.1.mybatis
SpringBoot官方并沒有提供Mybatis的啟動器,不過Mybatis官方自己實現了:
<!--mybatis --> <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version> </dependency>配置,基本沒有需要配置的:
# mybatis 別名掃描 mybatis.type-aliases-package=cn.itcast.pojo # mapper.xml文件位置,如果沒有映射文件,請注釋掉 mybatis.mapper-locations=classpath:mappers/*.xml需要注意,這里沒有配置mapper接口掃描包,因此我們需要給每一個Mapper接口添加@Mapper注解,才能被識別。
@Mapper public interface UserMapper { }user對象參照課前資料,需要通用mapper的注解:
接下來,就去集成通用mapper。
4.5.2.通用mapper
通用Mapper的作者也為自己的插件編寫了啟動器,我們直接引入即可:
<!-- 通用mapper --> <dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>2.0.2</version> </dependency>不需要做任何配置就可以使用了。
@Mapper public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User>{ }4.6.整合事務
其實,我們引入jdbc或者web的啟動器,就已經引入事務相關的依賴及默認配置了
至于事務,SpringBoot中通過注解來控制。就是我們熟知的@Transactional
@Service public class UserService {@Autowiredprivate UserMapper userMapper;public User queryById(Long id){return this.userMapper.selectByPrimaryKey(id);}@Transactionalpublic void deleteById(Long id){this.userMapper.deleteByPrimaryKey(id);} }4.7.啟動測試
在UserController中添加測試方法,內容:
@RestController @RequestMapping("user") public class UserController {@Autowiredprivate UserService userService;@GetMapping("{id}")public User queryUserById(@PathVariable("id")Long id){return this.userService.queryById(id);}@GetMapping("hello")public String test(){return "hello ssm";} }我們啟動項目,查看:
4.8.完整項目結構
完整的pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.itcast.user</groupId><artifactId>itcast-user</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.6.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--jdbc的啟動器,默認使用HikariCP連接池--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!--不要忘記數據庫驅動,因為springboot不知道我們使用的什么數據庫,這里選擇mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--mybatis --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><!-- 通用mapper --><dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>2.0.2</version></dependency></dependencies></project>完整的application.properties:
server.port=80logging.level.org.springframework=debugspring.datasource.url=jdbc:mysql://localhost:3306/heima spring.datasource.username=root spring.datasource.password=root# mybatis 別名掃描 mybatis.type-aliases-package=cn.itcast.pojo # mapper.xml文件位置,如果沒有映射文件,請注釋掉 # mybatis.mapper-locations=classpath:mappers/*.xml5.Thymeleaf快速入門
SpringBoot并不推薦使用jsp,但是支持一些模板引擎技術:
以前大家用的比較多的是Freemarker,但是我們今天的主角是Thymeleaf!
5.1.為什么是Thymeleaf?
簡單說, Thymeleaf 是一個跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。相較于其他的模板引擎,它有如下四個極吸引人的特點:
- 動靜結合:Thymeleaf 在有網絡和無網絡的環境下皆可運行,即它可以讓美工在瀏覽器查看頁面的靜態效果,也可以讓程序員在服務器查看帶數據的動態頁面效果。這是由于它支持 html 原型,然后在 html 標簽里增加額外的屬性來達到模板+數據的展示方式。瀏覽器解釋 html 時會忽略未定義的標簽屬性,所以 thymeleaf 的模板可以靜態地運行;當有數據返回到頁面時,Thymeleaf 標簽會動態地替換掉靜態內容,使頁面動態顯示。
- 開箱即用:它提供標準和spring標準兩種方言,可以直接套用模板實現JSTL、 OGNL表達式效果,避免每天套模板、改jstl、改標簽的困擾。同時開發人員也可以擴展和創建自定義的方言。
- 多方言支持:Thymeleaf 提供spring標準方言和一個與 SpringMVC 完美集成的可選模塊,可以快速的實現表單綁定、屬性編輯器、國際化等功能。
- 與SpringBoot完美整合,SpringBoot提供了Thymeleaf的默認配置,并且為Thymeleaf設置了視圖解析器,我們可以像以前操作jsp一樣來操作Thymeleaf。代碼幾乎沒有任何區別,就是在模板語法上有區別。
接下來,我們就通過入門案例來體會Thymeleaf的魅力:
5.2.提供數據
編寫一個controller方法,返回一些用戶數據,放入模型中,將來在頁面渲染
@GetMapping("/all") public String all(ModelMap model) {// 查詢用戶List<User> users = this.userService.queryAll();// 放入模型model.addAttribute("users", users);// 返回模板名稱(就是classpath:/templates/目錄下的html文件名)return "users"; }5.3.引入啟動器
直接引入啟動器:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>SpringBoot會自動為Thymeleaf注冊一個視圖解析器:
與解析JSP的InternalViewResolver類似,Thymeleaf也會根據前綴和后綴來確定模板文件的位置:
- 默認前綴:classpath:/templates/
- 默認后綴:.html
所以如果我們返回視圖:users,會指向到 classpath:/templates/users.html
一般我們無需進行修改,默認即可。
5.4.靜態頁面
根據上面的文檔介紹,模板默認放在classpath下的templates文件夾,我們新建一個html文件放入其中:
編寫html模板,渲染模型中的數據:
注意,把html 的名稱空間,改成:xmlns:th="http://www.thymeleaf.org" 會有語法提示
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>首頁</title><style type="text/css">table {border-collapse: collapse; font-size: 14px; width: 80%; margin: auto}table, th, td {border: 1px solid darkslategray;padding: 10px}</style> </head> <body> <div style="text-align: center"><span style="color: darkslategray; font-size: 30px">歡迎光臨!</span><hr/><table class="list"><tr><th>id</th><th>姓名</th><th>用戶名</th><th>年齡</th><th>性別</th><th>生日</th></tr><tr th:each="user : ${users}"><td th:text="${user.id}">1</td><td th:text="${user.name}">張三</td><td th:text="${user.userName}">zhangsan</td><td th:text="${user.age}">20</td><td th:text="${user.sex}">男</td><td th:text="${user.birthday}">1980-02-30</td></tr></table> </div> </body> </html>我們看到這里使用了以下語法:
- ${} :這個類似與el表達式,但其實是ognl的語法,比el表達式更加強大
- th-指令:th-是利用了Html5中的自定義屬性來實現的。如果不支持H5,可以用data-th-來代替
- th:each:類似于c:foreach 遍歷集合,但是語法更加簡潔
- th:text:聲明標簽中的文本
- 例如<td th-text='${user.id}'>1</td>,如果user.id有值,會覆蓋默認的1
- 如果沒有值,則會顯示td中默認的1。這正是thymeleaf能夠動靜結合的原因,模板解析失敗不影響頁面的顯示效果,因為會顯示默認值!
5.5.測試
接下來,我們打開頁面測試一下:
5.6.模板緩存
Thymeleaf會在第一次對模板解析之后進行緩存,極大的提高了并發處理能力。但是這給我們開發帶來了不便,修改頁面后并不會立刻看到效果,我們開發階段可以關掉緩存使用:
# 開發階段關閉thymeleaf的模板緩存 spring.thymeleaf.cache=false注意:
在Idea中,我們需要在修改頁面后按快捷鍵:`Ctrl + Shift + F9` 對項目進行rebuild才可以。eclipse中沒有測試過。我們可以修改頁面,測試一下。
總結
- 上一篇: java 重构 if else_项目中的
- 下一篇: 1-1 顺序表操作集 (20 分)