directory
Common and injected object data
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Set file encoding in settings in IDE
Properties use Getter and Setter, reflective mechanism
package edu.xiao.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ConfigurationProperties(prefix = "person")
public class QuickConfigurationAnnoController {
private String name;
private String address;
private Integer age;
@RequestMapping("/ConfigurationMethod")
public @ResponseBody String firstMethod(){
return "age:"+age+",name:"+name+",address: "+address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
It is recommended to use yml file format, there is a prompt when writing a good attribute
# server port
Server.Port = 8088
Server.servlet.context-path =/demo
Person.age = 18
Person.name = Zhang San
Person.address = Beijing
Effect display
When writing a good attribute, there will be a prompt function, but if Properties has the attribute, it will cover it
After comments on the Person property in the Properties file, create an application.yml file in the new Application.yml file
person:
name:age: 22
address:Hunan
Effect
Add @propertySource annotation, write the files to read, and enCoding