package com.example.initializr_test.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/test2")
@Component
//@Component注解表示将类交给spring作为bean进行管理;
@ConfigurationProperties(prefix = "girl")
//@ConfigurationProperties(prefix = "girl")注解表示读取application.yml文件中前缀为girl的配置
public class Test2Controller {
private String cupSize;
private Integer age;
public String getCupSize() {
return cupSize;
}
public Integer getAge() {
return age;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public void setAge(Integer age) {
this.age = age;
}
@GetMapping("/outputinfo")
public void outputinfo(){
System.out.println("cupSize:"+cupSize);
System.out.println("age:"+age);
}
}
application.yml
#冒号之后若有内容,需要有一个空格,这是yml规定的格式。
server:
port: 80
servlet:
context-path: /lizhengtonghe
#可以看到:在yml配置文件中并不需要指定属性的具体类型,只需要在注入的地方指定具体的类型即可
com.didispace.test.name: "zhuzaiming"
com.didispace.test.title: "springboot initializr"
test_dict: "com.didispace.test.name: ${com.didispace.test.name},com.didispace.test.title: ${com.didispace.test.title}"
girl:
cupSize: B
age: 18
加载中,请稍候......