SpringBoot获取配置文件中的内容

转载自:《springboot获取配置文件中的内容》

代码:

GrilApplication.java

@SpringBootApplication
public class GrilApplication {

    public static void main(String[] args) {
        SpringApplication.run(GrilApplication.class, args);
    }
}

application.yml

 server:
   port: 8089
 gril:
   cupSize: B
   age: 18

GrilProperties.java

@Component
@ConfigurationProperties(prefix = "gril")
public class GrilProperties {
    private String cupSize;
    private Integer age;

    public String getCupSize() {
        return cupSize;
    }

    public void setCupSize(String cupSize) {
        this.cupSize = cupSize;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 HelloController.java

@Controller
public class HelloController {

    @Autowired
    private GrilProperties grilProperties;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public  String say(){

        return grilProperties.getCupSize();
    }
}

访问地址http://127.0.0.1:8089/hello

 

猜你喜欢

转载自blog.csdn.net/yh_zeng2/article/details/82356459