1. 程式人生 > 其它 >SpringBoot 2.x 實戰仿B站高效能後端專案 學習總結

SpringBoot 2.x 實戰仿B站高效能後端專案 學習總結

SpringBoot 2.x 實戰仿B站高效能後端專案

學習完總結一下,記錄一下。

1 springboot簡介
它是有把spring的東西都封裝起來,使用大量註解,去掉了大量的xml配置。基於maven下能夠直接使用,只須要引用依賴。因此要學習springboot以前要先學習springmvc的spring,尤為須要瞭解其中的註解式開發。其中springboot內建了tomcat引入springbootweb的依賴就能夠存在controller層,而且能夠進行web開發。
springboot能夠快速構建,網址是start.spring.io,基於gradle或者maven均可以構建。
2springboot熱部署
這個須要引入devtools這個依賴,而且在application.properties中配置開啟熱部署。和關於修改那些檔案能夠自動重啟。
3springboot引擎模板
1 freemarker
即xxx.ftl檔案標頭檔案與html檔案同樣。裡面能夠使用jstl表示式。而且與springmvc同樣能夠配置字首字尾。和encoding。還有context格式。其中controller層開發與springmvc相同。
2 thymeleaf
這個模板能夠使用html模型。字尾能夠是.html即xxxx.html,其中能夠使用el表示式,如<h1 th:name=${name}> helloword</h1>

 其中th:須要在標頭檔案引入thymeleaf模板。
thymeleaf模板其中有一個日期格式轉換。html

<input th:value="${#dates.format(user.birthdaty,'yyyy-MM-dd')}">

其中使用#dates.format第一個引數是你controller傳過來的日期值,第二個引數是你要轉換成什麼格式。
th:text與th:utext的區別
th:text是後臺傳過來的帶有html元素的東西沒法解析。直接按照文字格式輸出到頁面上。
th:utext能將html元素解析出來。按格式顯示到頁面上。
url的寫法web

 <a th:href="@{http://www.baidu.com}">網站地址</a>

須要用@{xxxx}把網址引進去。
form的寫法基本同樣。spring

<form th:action="@{/th/postform}" th:object="${user}" th:method="post">
        <input type="text" th:field="*{name}">
        <input type="submit" th:value="提交">
    </form>

其中th:field=“name”這個意思是,這個input的id,name,value都與name同樣。如id=“name”
判斷th:if參照以下程式碼tomcat

<div th:if="${user.age} == 18">18</div>
    <div th:if="${user.age} gt 18">大於18</div>
    <div th:if="${user.age} lt 18">小於18</div>
    <div th:if="${user.age} ge 18">大於等於18</div>
    <div th:if="${user.age} le 18">小於等於18</div>

選擇也參照以下程式碼springboot

select>
        <option>選擇框</option>
        <option th:selected="${user.name eq 'ck'} ">ck</option>
        <option th:selected="${user.name eq 'aa'} ">aa</option>
        <option th:selected="${user.name eq 'bb'}">bb</option>
    </select>

其中當誰的值符合條件就將誰的值預設顯示出來
foreach與jstl基本相同mvc

table>
        <tr>
            <th>姓名</th>
            <th>年齡</th>
            <th>年齡備註</th>
            <th>生日</th>
        </tr>
        <tr th:each="person:${userlist}">
            <td th:text="${person.name}"></td>
            <td th:text="${person.age}"></td>
            <td th:text="${person.age  gt 18} ? 你老了:你很年輕" >18歲</td>
            <td th:text="${#dates.format(user.birthdaty,'yyyy-MM-dd hh:mm:ss')}"></td>
        </tr>
    </table>

userlist是後臺傳過來的list集合。
switch:caseapp

```
<div th:switch="${user.name}">
        <p th:case="ck">ck</p>
        <p th:case="#{roles.manager }">普通管理員</p>
        <p th:case="#{roles.superadmin }">超管</p>
        <p th:case="*">其餘使用者</p>
    </div>```
其中#{roles.xxxx}這個是建立的一個資原始檔.message.properties
裡邊
roles.manager=manager
roles.superadmin=superadmi

簡化部署

配置依賴:

<build>
 <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>

將這個應用打包成jar包,可以直接使用java -jar 命令執行。 ##POM檔案

父專案

<parent>
 <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> </parent>
上面的父專案是
<parent>
 <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.4.RELEASE</version></parent>
它來管理SpringBoot應用裡的所有依賴版本

啟動器

<dependency>
 <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>

spring-boot-starter-web: spring-boot-starter是springboot的場景啟動器,幫我們匯入web依賴。
SpringBoot將所有的功能場景都一個個的抽取出來,成為starts(啟動器),只需要在專案總引入這些start,相關功能場景的所以依賴都會打入進來。

主啟動類,主入口類

@SpringBootApplication
public class SpringbootDemo1Application {
 public static void main(String[] args) { SpringApplication.run(SpringbootDemo1Application.class, args); }
}

@SpringBootApplication:此註解標註的類,就是SpringBoot的主配置類,SpringBoot就是通過這個類的main方法啟動應用。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
 excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class}), @Filter(
 type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class})}
)
public @interface SpringBootApplication {
  • @SpringBootConfiguration:SpringBoot的配置類

    • @Configuration: 標註的類為配置類

      • @Component:
  • @EnableAutoConfiguration:開啟自動配置功能

    • @AutoConfigurationPackage:自動配置包
    • @Import({AutoConfigurationImportSelector.class})
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

將主配置類所在包下及子包下里的說有元件掃描到spring容器

AutoConfigurationImportSelector 匯入哪些元件的選擇器

SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
SpringBoot在啟動的時候從類路徑下的META-INF/spring.factories中獲取AutoConfiguration指定的值,將這些值作為自動裝配類匯入到容器中,自動裝配類就生效