Spring boot 读取配置文件, 有如下几种方法
1. 直接利用Spring @Value 注解
2. 利用@PropertySource("classpath:xxx.properties") 与 @Value 注解配合
3. @PropertySource("classpath:xxx.properties") 与 @ConfigurationProperties 注解配合
采用这三种常见的方法,甚至可以直接用配置文件组装复杂的对象都可以
一,直接利用Spring @Value 注解
直接在使用的类中使用,比如:
程序代码
@Service
public class MyCommandService {
@Value("${name:unknown}")
private String name;
public String getMessage() {
return getMessage(name);
}
public String getMessage(String name) {
return "Hello " + name;
}
}
二. 利用@PropertySource("classpath:xxx.properties") 与 @Value 注解配合
程序代码
package com.yihaomen.springboot;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:application.properties")
public class GlobalProperties {
@Value("${name}")
private String name;
@Value("${address}")
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
三. @PropertySource("classpath:xxx.properties") 与 @ConfigurationProperties 注解配合
程序代码
package com.yihaomen.springboot;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties
public class GlobalProperties {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
这样做了之后,在其他类中怎么调用呢,其实直接注入就可以了。 记住我们用 @Component 来注解了这个properties 类,所以可以在其他类中用 @Autowired 注入:
程序代码
package com.yihaomen.springboot.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.yihaomen.springboot.GlobalProperties;
@Service
public class MyCommandService {
@Value("${name:unknown}")
private String name;
@Autowired
private GlobalProperties prop;
public String getMessage() {
return getMessage(name);
}
public String getMessage(String name) {
return "Hello " + name + " || address: " + prop.getAddress();
}
}
如何mapping 一个复杂的对象呢,比如有如下一个配置文件:
程序代码
name=yihaomen-aaa
address=wuhan
#App
app.menus[0].title=Home
app.menus[0].name=Home
app.menus[0].path=/
app.menus[1].title=Login
app.menus[1].name=Login
app.menus[1].path=/login
app.compiler.timeout=5
app.compiler.output-folder=/temp/
app.error=/error/
注解得到对应的类如下:
程序代码
@Component
@ConfigurationProperties("app") // 前缀找 app.* , 没有前缀,找根
public class AppProperties {
private String error;
private List<Menu> menus = new ArrayList<>();
private Compiler compiler = new Compiler();
public static class Menu {
private String name;
private String path;
private String title;
//getters and setters
@Override
public String toString() {
return "Menu{" +
"name='" + name + '\'' +
", path='" + path + '\'' +
", title='" + title + '\'' +
'}';
}
}
public static class Compiler {
private String timeout;
private String outputFolder;
//getters and setters
@Override
public String toString() {
return "Compiler{" +
"timeout='" + timeout + '\'' +
", outputFolder='" + outputFolder + '\'' +
'}';
}
}
//getters and setters
}
spring boot properties 源码下载:
spring boot properties sample download