一、读取核心配置文件
核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单。
核心配置文件application.properties内容如下:
server.port=9090
test.msg=Hello World Springboot!
1、使用@Value方式(常用):
@RestController
public class WebController {
@Value("${test.msg}")
private String msg;
@RequestMapping(value = "index", method = RequestMethod.GET)
public String index() {
return "The Way 1 : " +msg;
}
}
注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody。
访问:http://localhost:9090/index 时将得到The Way 1 : Hello World Springboot!
2、使用Environment方式
@RestControl