Spring boot to read the configuration of the way (specify the configuration file)

Part Speaking Spring boot reads the configuration file for the main convention application files and named Road King profile.

Way to read this article Road King of the specified configuration file.

Suppose you specify the configuration file name and the Road King: config / wx.properties:

Contents:

wx.appKey=Test
wx.appSecret=abcdefghik

1. @ PropertySource + @ Value Notes


@Component
@PropertySource(value = { "config/wx.properties" })
public class wxConfig{
 
   @Value("${wx.appKey}")
   private String appKey;
 
   @Value("${wx.appSecret}")
   private String appSecret;
 

 
   get和set方法...

}

Note: @PropertySource yml does not support file read.

 

2.@PropertySource+@ConfigurationProperties注解

@Component
@ConfigurationProperties(prefix = "wx")
@PropertySource(value = { "config/wx.properties" })
public class wxConfig{
 
   private String appKey;
   private String appSecret;
 
   get和set方法...
 
}

 

3.Environment read

All through out the basic load profile configuration will get to be injected through the Environment.


@Autowired
private Environment env;
 
// 获取参数
String getProperty(String key);

 

Guess you like

Origin blog.csdn.net/syilt/article/details/92208860