【springBoot 2.x】開發實戰day1,目錄結構、常用配置以及日誌的選擇和配置

目錄結構

  1. 目錄結構中,main/resources下的三個必備文件
    ① static 靜態資源文件存放路徑(圖片、CSS、JS等)
    ② templates 存放模版文件
    ③ application.properties 配置文件

  2. @SpringBootApplication 啓動程序時會自動加載該包及其子包下的類

@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 {
	//省略...
}

可以看到@SpringBootApplication是一個複合註解,包括@ComponentScan,@SpringBootConfiguration,@EnableAutoConfiguration
@ComponentScan
掃描當前包及其子包下被 @Component@Controller@Service@Repository註解標記的類並納入到spring容器中進行管理

@SpringBootConfiguration
繼承自@Configuration,二者功能也一致,標註當前類是配置類,並會將當前類內聲明的一個或多個以@Bean註解標記的方法的實例納入到srping容器中,並且實例名就是方法名
@EnableAutoConfiguration
主要作用也是將Bean加入到spring容器中,先不說與@ComponentScan的區別。


常用相關配置(會持續更新)

配置 說明
server.port 應用服務器端口
server.servlet.context-path 應用上下文 localhost:8888/mySpringBoot
spring.http.encoding.charset 默認字符集編碼
spring.thymeleaf.cache 開啓thymeleaf緩存
spring.mvc.dateFormat MVC中對輸入日期參數格式化
spring.jackson.date-format 日期格式:默認使用格林納尼治時間
spring.jackson.time-zone 設置JSON日期序列化輸出格式,北京時間相對倫敦有8個小時時差所以使用GMT+8
spring.servlet.multipart.max-file-size 單個文件最大尺寸
spring.servlet.multipart.max-request-size 一個請求最大尺寸
spring.servlet.multipart.location 臨時文件目錄
spring.mvc.static-path-pattern 配置靜態資源訪問前綴
logging.file 日誌輸出的地址
logging.level 日誌級別 debug --> info --> warn --> error 默認爲info,如果設置了debug=true時,會將日誌自動降級到debug級別
logging.level.root ROOT 表示全局設置
logging.level.org.springframework 設置指定包的輸出級別 eg 指定org.springframework包級別爲error
debug=false debug 開啓/關閉調試模式

解釋和注意事項(當前後期我們會使用yml語言來進行配置)

#server
# 端口
server.port=8888
# 應用上下文  localhost:8888/mySpringBoot
# 一般情況下,小項目通常是在tomcat下部署多個webapp,通過上下文來區分
# 在集羣或者大中型項目中,通常我們一個tomcat對應一個webapp,然後通過不同接口來進行區分
server.servlet.context-path=/mySpringBoot


# spring
# 默認字符集編碼 UTF-8 只包含了20000+中文字符,生僻字顯示不了。
spring.http.encoding.charset=utf-8
# 開始thymeleaf緩存(修改頁面刷新後沒有變化...)
# 注意的是:除了設置cache = false外。還要設置IDEA file --> setting --> build -->complier --> Build Project auto.. 勾選才可以實現熱部署
spring.thymeleaf.cache=false
#
# MVC中對輸入日期參數格式化
spring.mvc.dateFormat=yyyy-MM-dd 

# 日期格式(yyyy-MM-dd,默認使用格林納尼治時間,我們需要將它指定,否則輸出日期格式不對,所以指定json序列化格式)
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss SSS
# 設置JSON日期序列化輸出格式,北京時間相對倫敦有8個小時時差所以使用GMT+8
spring.jackson.time-zone=GMT+8

# LOG 日誌配置 (由於springBoot自帶日誌配置粒度不能滿足生產環境,我們可以將logback日誌配置文件直接放在resource下)
# 日誌輸出的地址,springboot默認沒有進行文件輸出,只在控制檯中進行了打印
#logging.file=E:/logs/mySpringBoot.log
# 日誌級別   debug --> info --> warn --> error 默認爲info,如果設置了debug=true時,會將日誌自動降級到debug級別
# ROOT 表示全局設置
#logging.level.root=info
# 設置指定包的輸出級別 eg 指定org.springframework包級別爲error
#logging.level.org.springframework = error

# debug 開啓/關閉調試模式。
debug=false

實際開發中,日誌不會以這種配置形式出現,我們會將Log日誌配置,單拿出來放在一個單獨的配置文件,放在/resource下即可, springboot會自動識別。


<!--定義日誌保存的路徑-->
<property name="LOG_HOME" value="E:/logs/prd/" />

<!--定義一個控制檯輸出器,名爲console-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
   <!--按pattern指定的格式輸出日誌,編碼爲UTF-8-->
   <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread]  %logger{30} - %msg%n</pattern>
      <charset>UTF-8</charset>
   </encoder>
</appender>

<!--定義一個日滾動(每天生成一份)的日誌文件-->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
   <!--按pattern指定的格式輸出日誌,編碼爲UTF-8-->
   <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
      <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread]  %logger{30} - %msg%n</pattern>
      <charset>UTF-8</charset>
   </encoder>
   <!-- 定義保存的文件名 -->
   <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!--%d{yyyy-MM-dd}代表每天生成一個新的日誌-->
      <fileNamePattern>${LOG_HOME}/mysprintboot_%d{yyyy-MM-dd}.log</fileNamePattern>
      <!--日誌最多保存90天,也就是90份-->
      <maxHistory>90</maxHistory>
   </rollingPolicy>
   <!-- 在日滾動文件中,強制只保存錯誤INFO級別以上信息 -->
   <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>INFO</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
   </filter>
</appender>

<!-- 定義日誌全局最低輸出級別是INFO,同時向控制檯和日滾動文件輸出 -->
<root level="INFO">
   <appender-ref ref="console" />
   <appender-ref ref="file" />
</root>

ok。 這個日誌配置文件還是很不錯的,我們也可以對這個日誌配置稍作修改,添加判斷來確定dev、pro兩個環境根據主配置來自動識別,添加配置文件,這樣我們就不用在生產與開發環境進行手動切換,我會將這個日誌放在Day2中,Day2也會記錄yaml語言的基本用法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章