讀取application.yml文件中的自定義屬性
方式1:
application.yml文件中的自定義屬性
rsa:
key:
pubKeyFile: D:/document/key/key_rsa.pub
filter:
allowPaths:
- /auth/login
- /auth/code
- /auth/sms
- /auth/send
- /upload/image
在對應的微服務中創建與自定義屬性相符的屬性配置類
@Data
@ConfigurationProperties("rsa.key")
public class RsaKeyProperties {
private String pubKeyFile;
public PublicKey publicKey;
/**
* 在創建對象之後執行的方法
*
* @throws Exception
*/
@PostConstruct
public void createRsaKey() throws Exception {
publicKey = RsaUtils.getPublicKey(pubKeyFile);
}
}
在要使用自定義屬性的類上使用@EnableConfigurationProperties(RssaKeyProperties.class),或者直接將該註解在啓動類上使用,這樣就可以在該微服務中都可以使用該屬性配置類。
方式2:
指定在要使用自定屬性的類中(注意別導錯包)
org.springframework.beans.factory.annotation.Value
@Value("${rsa.key.publicFileKey}")
private String publicFileKey;
@Value("${rsa.key.filter.allowPaths}")
private List<String> allowPaths;