读取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;