读取application.yml文件中的自定义属性

读取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;
发布了15 篇原创文章 · 获赞 83 · 访问量 8271

猜你喜欢

转载自blog.csdn.net/weixin_42339552/article/details/103367393
今日推荐