一般而言,SpringBoot项目中都会有一个默认的配置文件Application.Properties,相当于一个变量的列表,有点类似于系统启动时的*.ini文件,一般都是放在Resources文件夹下面。
那么在程序中,又是怎么读取和使用Application.Properties里面的配置信息的呢?一般有三种方式:
1. @Value
一般在程序启动类里使用,相当于声明和初始化了某个属性。
Application.Properties配置文件信息如下:
com.zyd.type=Springboot - @Value
com.zyd.title=使用@Value获取配置文件
在启动类中加入@Value("${com.zyd.type}")
public class Applaction {
@Value("${com.zyd.type}")
private String type;
@Value("${com.zyd.title}")
private String title;
@RequestMapping("/value")
public Map value() throws UnsupportedEncodingException {
Map map = new HashMap();
map.put("type", type);
// *.properties文件中的中文默认以ISO-8859-1方式编码,因此需要对中文内容进行重新编码
map.put("title", new String(title.getBytes("ISO-8859-1"), "UTF-8"));
return map;
}
public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(Applaction.class);
application.run(args);
}
}
2. Environment
一般在程序启动类里使用,相当于通过这个类的方法获取到属性信息。
Application.Properties配置文件信息如下:
com.zyd.type=Springboot - @Value
com.zyd.title=使用@Value获取配置文件
在启动类中可以使用Environment获取配置信息。
....
import org.springframework.core.env.Environment;
....
public class Applaction {
@Autowired
private Environment env;
@RequestMapping("/env")
public Map env() throws UnsupportedEncodingException {
Map map = new HashMap();
map.put("type", env.getProperty("com.zyd.type"));
map.put("title", new String(env.getProperty("com.zyd.title").getBytes("ISO-8859-1"), "UTF-8"));
return map;
}
public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(Applaction.class);
application.run(args);
}
}
3. @ConfigurationProperties
一般在自定义类里使用,相当于通过自定义类的方法获取到属性信息,前提是要在自定义类中设置相应的方法获取配置信息。
自定义类
public class PropertiesConfig {
public String type;
public String title;
public Map login = new HashMap();
public List urls = new ArrayList<>();
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTitle() {
try {
return new String(title.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Map getLogin() {
return login;
}
public void setLogin(Map login) {
this.login = login;
}
public List getUrls() {
return urls;
}
public void setUrls(List urls) {
this.urls = urls;
}
}
使用自定义类读取配置信息
....
import com.zyd.property.config.PropertiesConfig;
....
public class Applaction {
@Autowired
private PropertiesConfig propertiesConfig;
@RequestMapping("/config")
public Map configurationProperties() {
Map map = new HashMap();
map.put("type", propertiesConfig.getType());
map.put("title", propertiesConfig.getTitle());
map.put("login", propertiesConfig.getLogin());
map.put("urls", propertiesConfig.getUrls());
return map;
}
public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(Applaction.class);
application.run(args);
}
}
当然,以上的读取的都是默认的配置文件,还可以通过加入注册监听器的方式加载配置文件,即是PropertiesLoaderUtils方法。