通过注解@ConfigurationProperties(prefix="配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映射,试例

application.properties配置如下:

person:
  name: xiaoming
  age: 10

或者,application.yml配置如下:

person:
  name: xiaoming
  age: 10

实体Bean代码如下:

@Controller
@ConfigurationProperties(prefix = "person")
public class QuickStartController {

    private String name;
    private Integer age;

    @RequestMapping("/test01")
    @ResponseBody
    public String test01(){
        return "springboot 访问成功! name="+name+",age="+age;
    }

    public void setName(String name) {
        this.name = name;
    }

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

pom.xml中配置@ConfigurationProperties的执行器的依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>
Last modification:April 18th, 2019 at 07:18 pm
如果觉得这篇技术文章对你有用,请随意赞赏