SpringBoot 常用特性總結

ConfigurationProperties

SpringBoot 的一個很重要的功能是外部化配置,可以直接訪問配置文件(application.yml)中定義的字段值,並能夠完成屬性綁定。

但是,需要注意:@ConfigurationProperties 並沒有把當前類註冊成爲一個 Spring 的 Bean。所以,我們在使用時都會配合 @Component 註解直接進行注入。例如:

@Data
@Component
@ConfigurationProperties(prefix = "adconf.mysql")
public class MySQLConfig {

    private String host;
    private Integer port;
    private String username;
    private String password;
}

注意到,ConfigurationProperties 的 prefix 屬性指定了 adconf.mysql。所以,它會映射到 application.yml 中對應的字段。例如:

adconf:
  mysql:
    host: 127.0.0.1
    port: 3306
    username: root
    password: Djangobbs

CommandLineRunner 與 ApplicationRunner

CommandLineRunner 與 ApplicationRunner 接口是在 Spring 容器啓動之後做的一些自定義操作。

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