SpringBoot配置文件与配置类的属性映射方式

一、在pom文件中加入依赖

目录结构

SpringBoot配置文件与配置类的属性映射方式_第1张图片
在实体类中会出现错误,然后点击这个网址会有需要的依赖
网址:
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor
引入的依赖为:


             org.springframework.boot 
             spring-boot-configuration-processor 
             true 
        


    4.0.0
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    com.william
    day01_springboot_initializr
    0.0.1-SNAPSHOT
    day01_springboot_initializr
    Demo project for Spring Boot

    
        1.8
    

    
        
             org.springframework.boot 
             spring-boot-configuration-processor 
             true 
        
        
        
            org.springframework.boot
            spring-boot-devtools
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



二、application.yml

#配置对象
person:
  name: zhangsan
  age: 19
  date: 2019/09/09
  # 数组配置
  city:
  - beijing
  - tianjin
  - shanghai
  - chongqing

# 配置集合
student:
- name: zhangsan
  age: 18
  score: 100
- name: lisi
  age: 28
  score: 88
- name: wangwu
  age: 38
  score: 90
#注意事项:在key与value直接需要加入空格,对大小写敏感。
server:
  port: 8081
  servlet:
    context-path: /demo

三、application.properties

server.port=8081
server.servlet.context-path=/demo

四、Person

需要提供get/set方法 和toString方法

还需要注解@Component

需要注解@ConfigurationProperties(prefix = “person”)

使用注解@ConfigurationProperties映射

通过注解@ConfigurationProperties(prefix=’'配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映 射。
使用@ConfigurationProperties方式必须提供Setter方法,使用@Value注解不需要Setter方法

package com.william.day01_springboot_initializr.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;

/**
 * @author :lijunxuan
 * @date :Created in 2019/6/27  19:29
 * @description :
 * @version: 1.0
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private String age;
    private Date date;
    private String []  city;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", date=" + date +
                ", city=" + Arrays.toString(city) +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String[] getCity() {
        return city;
    }

    public void setCity(String[] city) {
        this.city = city;
    }
}

五、HelloController

1、使用注解@Value映射

@value注解将配置文件的值映射到Spring管理的Bean属性值

package com.william.day01_springboot_initializr.controller;

import com.william.day01_springboot_initializr.domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :lijunxuan
 * @date :Created in 2019/6/27  17:20
 * @description :
 * @version: 1.0
 */
@RestController
public class HelloController {
    @Autowired
    Person person;
    @Value("${person.name}")
    private String name;
    @Value("${person.age}")
    private String age;

    @RequestMapping("/hello")
    public String hello(){
        return String.format("hello world  你好!世界!1234222 name = %s!! age = %s!! person = %s !!",name,age,person);
       // return String.format("hello world  你好!世界!1234222 person = %s !!",person);
    }
}

七、测试 结果

在这里插入图片描述

你可能感兴趣的:(SpringBoot)