一) 读取默认的application.properties或application.yml
1. 使用@Value
@Value("${sql.name}") private String sqlName;2. 使用Environment
@Autowired
private Environment env
String sqlName = env.getProperty("sql.name");
二)使用@ConfigurationProperties读取自定义配置文件@ConfigurationProperties(locations = "classpath:config/my-web.properties", prefix = "sql")
@Component
public class MyWebConfig {
private String name;
public String getName() {
return name;
}
public String setName(String name){
this.name = name;
}
}
调用:
@Autowired
private MyWebConfig config;
String sqlName = config.getName();