#SpringBoot# SpringBoot熱部署devtool和配置文件自動注入實戰

SpringBoot2.x使用Dev-tool熱部署

什麼是熱部署,使用springboot結合dev-tool工具,快速加載啓動應用

官方地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools
核心依賴包:
    <dependency>  
         <groupId>org.springframework.boot</groupId>  
         <artifactId>spring-boot-devtools</artifactId>  
         <optional>true</optional>  
     </dependency>
添加依賴後,在ide裏面重啓應用,後續修改後馬上可以生效
​
classloader

不被熱部署的文件 1、/META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates 2、指定文件不進行熱部署 spring.devtools.restart.exclude=static/,public/ 3、手工觸發重啓 spring.devtools.restart.trigger-file=trigger.txt 改代碼不重啓,通過一個文本去控制

https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools-restart-exclude

注意點:生產環境不要開啓這個功能,如果用java -jar啓動,springBoot是不會進行熱部署的

SpringBoot2.x配置文件講解

SpringBoot2.x常見的配置文件 xml、yml、properties的區別和使用

  • xml、properties、json、yaml
  • 常見的配置文件 xx.yml, xx.properties,
    • YAML(Yet Another Markup Language) 寫 YAML 要比寫 XML 快得多(無需關注標籤或引號) 使用空格 Space 縮進表示分層,不同層次之間的縮進可以使用不同的空格數目 注意:key後面的冒號,後面一定要跟一個空格,樹狀結構 application.properties示例 server.port=8090
      server.session-timeout=30
      server.tomcat.max-threads=0
      server.tomcat.uri-encoding=UTF-8
  • application.yml示例 server:
    port: 8090
    session-timeout: 30
    tomcat.max-threads: 0
    tomcat.uri-encoding: UTF-8
  • 默認示例文件僅作爲指導。 不要將整個內容複製並粘貼到您的應用程序中,只挑選您需要的屬性。
  • 參考:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#common-application-properties
  • 如果需要修改,直接複製對應的配置文件,加到application.properties裏面

SpringBoot註解配置文件自動映射到屬性和實體類

使用@value註解配置文件自動映射到屬性和實體類

  • 配置文件加載
    • 方式一
      • 1、Controller上面配置 @PropertySource({“classpath:resource.properties”})
      • 2、增加屬性 @Value("${test.name}") private String name;
    • 方式二:實體類配置文件
      • 1、添加 @Component 註解;
      • 2、使用 @PropertySource 註解指定配置文件位置;
      • 3、使用 @ConfigurationProperties 註解,設置相關屬性;
      • 4、必須 通過注入IOC對象Resource 進來 , 才能在類中使用獲取的配置文件值。 @Autowired private ServerSettings serverSettings;
例子:
@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:resource.properties")
public class ServerConstant {
​
常見問題:
1、配置文件注入失敗,Could not resolve placeholder
解決:根據springboot啓動流程,會有自動掃描包沒有掃描到相關注解, 
默認Spring框架實現會從聲明@ComponentScan所在的類的package進行掃描,來自動注入,
因此啓動類最好放在根路徑下面,或者指定掃描包範圍
spring-boot掃描啓動類對應的目錄和子目錄
2、注入bean的方式,屬性名稱和配置文件裏面的key一一對應,就用加@Value 這個註解
如果不一樣,就要加@value("${XXX}")
第四章 Springboot2.0單元測試進階實戰和自定義異常處理
第一集 SpringBootTest單元測試實戰
簡介:講解SpringBoot的單元測試

//1、引入相關依賴
 <!--springboot程序測試依賴,如果是自動創建項目默認添加-->
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
//使用
@RunWith(SpringRunner.class)  //底層用junit  SpringJUnit4ClassRunner
@SpringBootTest(classes={XdclassApplication.class})//啓動整個springboot工程
public class SpringBootTests { }

在這裏插入圖片描述
公衆號: 自學it的攻城獅(study458)

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