SpringBoot读取配置文件的方式
application.yml
my.name: dada
使用Environment方式
@RequestMapping("/test")
@RestController
public class UserController {
@Autowired
private Environment env;
@GetMapping("/hello")
public String hello(){
String name = env.getProperty("my.name");
return name;
}
使用@Vaule方式
@RequestMapping("/test")
@RestController
public class UserController {
@Value("${my.name}")
String name;
@GetMapping("/hello")
public String hello(){
return name;
}
使用prefix方式
MyConfig.class
package com.dada.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 〈配置类〉
*
* @author hupengda
* @create 2019/1/10
*/
@ConfigurationProperties(prefix = "my")
@Component
public class MyConfig {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserController
@RequestMapping("/test")
@RestController
public class UserController {
@Autowired
MyConfig myConfig;
@GetMapping("/hello3")
public String hello3(){
return myConfig.getName();
}