@ConfigurationProperties(prefix = "")来绑定属性

yml:

server:
  port: 80

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/urp?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: root
mybatis:
  mapper-locations:

#obj
person:
  name: 刘备
  sex: 男
  age: 50

#  map
teacher:
  map:
    name: 刘老师
    sex: 男
    age: 50

#  list
testlist:
   list:
       - 1
       - 1
       - 2

 

属性:

package com.shiro.demo.entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * desc:
 * author CDN
 * create 2019-09-16 21:45
 * version 1.0.0
 */
//@PropertySource("")
@EnableConfigurationProperties(Person.class)
@ConfigurationProperties(prefix = "person")
@Component
public class Person {

    private String name;
    private String sex;
    private int age;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

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



}

获取List

package com.shiro.demo.entity;

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

import java.util.ArrayList;
import java.util.List;

/**
 * desc:
 * author CDN
 * create 2019-09-16 23:15
 * version 1.0.0
 */

//@PropertySource("")
@EnableConfigurationProperties(MyList.class)
@ConfigurationProperties(prefix = "testlist.list")
@Component
public class MyList {

    private List list=new ArrayList<>();

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
}

获取map

package com.shiro.demo.entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * desc:map
 * author CDN
 * create 2019-09-16 23:07
 * version 1.0.0
 */
//@PropertySource("")
@EnableConfigurationProperties(Teacher.class)
@ConfigurationProperties(prefix = "teacher")
@Component
public class Teacher {

    Map map=new HashMap<>();

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }
}

测试:

package com.shiro.demo.controller;

import com.shiro.demo.entity.MyList;
import com.shiro.demo.entity.Person;
import com.shiro.demo.entity.Teacher;
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;

/**
 * desc:获取配置文件的属性值
 * author CDN
 * create 2019-09-16 21:48
 * version 1.0.0
 */
@RestController
public class ObjController {

    @Value("${person.name}")
    private String name;

    @Value("${person.sex}")
    private String sex;

    @Value("${person.age}")
    private int age;

    @Autowired
    Person person;

    @Autowired
    Teacher teacher;


    @Autowired
    MyList myList;

    /**
     * desc:
     * param:
     * author: CDN
     * date: 2019/9/16
     */
    @RequestMapping("a")
    public String a() {
        System.out.println(name);
        System.out.println(person);
        System.out.println(teacher);
        System.out.println(myList);
        return "成功";
    }
}

 

你可能感兴趣的:(springboot)