了解自动配置绑定原理!
结构如下:
在MainApplication同级中创建一个包,包下文件名为:Person.java
package com.jin.boot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "mybaby")
public class Person {
private String name;
private Integer age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
", gender='" + gender + ''' +
'}';
}
}
在配置文件application.properties中绑定属性:
server.port=8888
mybaby.name="xiaohua"
mybaby.age=24
mybaby.gender="nv"
在MainApplication同级中创建一个包,包下文件名为:HelloController.java
package com.jin.boot.controller;
import com.jin.boot.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
Person person;
@RequestMapping("/my")
public Person getPerson() {
return person;
}
}
按快捷键Shift+F10(点击如下如按钮),运行!
显示效果如下(用了json插件):