Spring boot读取配置的方式

Spring boot读取配置的文件主要为约定俗成的application文件和指定路劲的配置文件。

Spring boot为读取application文件的配置提供一些方便的方法,主要有以下几种。

假设application文件的内容有:

wx.appKey=Test
wx.appSecret=abcdefghik

1.Value注解

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class appConfig{
 
   @Value("${app.appKey}")
   private String appKey;
 
   @Value("${app.appSecret}")
   private String appSecret;

 
    get和set方法...
}

2.@ConfigurationProperties注解


@Component
@ConfigurationProperties(prefix = "wx")
public class appConfig{
 
   private String appKey;
   private String appSecret;
  
   get和set方法...
 
}

猜你喜欢

转载自blog.csdn.net/syilt/article/details/92206302