1. 程式人生 > >SpringBoot如何將類中屬性與配置檔案中的配置進行繫結

SpringBoot如何將類中屬性與配置檔案中的配置進行繫結

1 匯入配置檔案處理器,配置檔案進行繫結就會有提示。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

2 在類上加@ConfigurationProperties(prefix="字首")註解。

3 用@Component註解將類注入到SpringBoot容器中。

支援JSR303校驗,類上加@Validated註解