springboot學習總結

springboot學習總結

入口類

由於Spring Boot會自動掃描@SpringBootApplication所在類的同級包(如com.example.demo)以及下級包裏的所有bean。所以,官方建議入口類放在最外層的包名下,即@SpringBootApplication所在類的所在目錄應包含整個程序的代碼部分。

如下圖所示:
這裏寫圖片描述

自定義配置

spring boot可簡化spring搭建的繁瑣,但也可以根據自身項目的需求進行修改。

配置文件實現步驟:

1.新建類繼承自WebMvcConfigurerAdapter可修改靜態資源的各種屬性

2.類使用@Configuration進行註解
這裏寫圖片描述

配置文件

在application.properties需要對一些參數進行配置

例如如果使用了postgresql支持包,就需要在application.properties中對postgresql進行配置,如下

datasource.dbregistro.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/tl_test
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driverClassName=org.postgresql.Driver

控制器

Controller

對頁面控制器訪問使用@Controller,在return時會自動查找templates下對應視圖

RestController

對接口控制器訪問時使用@RestController,在return時會返回對應json數據

發佈了35 篇原創文章 · 獲贊 14 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章