SpringBoot(2) | 配置文件

環境信息

系統 版本
SpringBoot 2.1.8.RELASE
Spring 5.1.9.RELEASE
JDK 1.8+
Maven 3.3+

配置文件

配置文件優先級

SpringBoot中默認使用application.properties和application.yml,yml格式有層次感,使用方便 其中,appcation.properties的優先級高於application.ymlhtml

配置文件默承認以放在下面4個位置git

1.項目根目錄下的config目錄github

2.項目根目錄下web

3.resources/config目錄下spring

4.resources目錄下bash

優先級從上到下,依次下降app

自定義配置文件

配置文件默認存在於上述4個位置,默認名稱爲application,能夠自定義用戶目錄和名稱ide

自定義路徑: spring.config.location=classpath:/myConfig/ ;ui

自定義配置文件名稱 spring.config.name=myConfigthis

屬性注入

使用@Value註解

編寫Student實體類com.xyz.entity.Student

package com.xyz.entity;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Repository;

//@Repository實現DAO層的Bean掃描
@Repository
//@PropertySource引入外部配置文件,結合@Value註解完成屬性的值注入
@PropertySource("/config/student.properties")
public class Student {

    @Value("${student.name}")
    private  String name;
    @Value("${student.address}")
    private String address;
    @Value("${student.age}")
    private int age;
    @Value("${sex}")
    private String sex;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' + ", address='" + address + '\'' + ", age=" + age + ", sex='" + sex + '\'' +
                '}';
    }
}
複製代碼

使用配置文件config/student.properties

student.name=xyz
student.address=bj
student.age=1
sex=0
複製代碼

編寫rest訪問入口com.xyz.controller.StudentController

package com.xyz.controller;

import com.xyz.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
//@RestController是Spring4以後引入的新註解,等於@ResponseBody+@Controller
public class StudentController {

    @Autowired
    private Student student;

    @RequestMapping("/student")
    public String student() {
        return "student:" + student;
    }
}

複製代碼

經過 http://localhost:8083/student查看結果

使用@ConfigurationProperties註解

編寫Teacher實體類com.xyz.entity.Teacher

package com.xyz.entity;

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


@Repository
@PropertySource("/config/teacher.properties")
//使用@ConfigurationProperties須要配置get和set方法,默認不須要prefix,若是變量值存在共同的前綴,能夠使用prefix
//@ConfigurationProperties(prefix = "teacher")
@ConfigurationProperties
public class Teacher {

    private String name;
    private int age;
    private String className;
    private String homeAddress;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getHomeAddress() {
        return homeAddress;
    }

    public void setHomeAddress(String homeAddress) {
        this.homeAddress = homeAddress;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' + ", age=" + age + ", className='" + className + '\'' + ", homeAddress='" + homeAddress + '\'' +
                '}';
    }
}

複製代碼

使用配置文件config/teacher.properties

#teacher.name=gzf
#teacher.age=100
#teacher.classname=248
#teacher.homeaddress=sxyc


name=gzf
age=100
classname=248
homeaddress=sxyc

複製代碼

編寫rest訪問接口com.xyz.controller.TeacherController

package com.xyz.controller;

import com.xyz.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TeacherController {

    @Autowired
    private Teacher teacher;

    @RequestMapping("/teacher")
    public String teacher() {
       return "teacher:" + teacher;
    }
}

複製代碼

經過http://localhost:8083/teacher查看結果

總結

SpringBoot中@Value註解和@ConfigurationProperties註解的區別

@ConfigurationProperties @Value
功能 批量注入配置文件中的屬性 單個指定
鬆散綁定(鬆散語法) 支持 不支持
spEL 不支持 支持
JSR303數據校驗 支持 不支持
複雜類型封裝 支持 不支持

參考www.cnblogs.com/slowcity/p/…

多環境配置文件

在實際應用中,須要不一樣的配置環境,格式爲application-{profile}.properties,profile表明環境標識,如:

application-dev.properties

application-test.properties

application-prod.properties

使用時在application.properties中增長spring.profiles.active=prod 對應application-prod.properties內容爲: server.port=8091

代碼目錄github.com/buyimoutian…

相關文章
相關標籤/搜索