1. 程式人生 > >《微服務之SpringBoot配置檔案詳解》

《微服務之SpringBoot配置檔案詳解》

前言:

       SpringBoot採用了構建生產就緒Spring應用程式的觀點,旨在讓程式快速啟動和執行。在一般情況下,不需要做太多的配置就能夠讓SpringBoot程式正常執行。在一個特殊情況下,我們需要修改一些配置,或者需要有自己的配置。

正文:

        一。自定義屬性

       (1)定義配置檔案

my:
   name: forezp
   age: 12

        (2)使用註解讀取配置檔案資訊[email protected]("${屬性名}")

package com.forezp.helloworld;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class MiyaController {
    @Value("${my.name}")
    private String name;
    @Value("${my.age}")
    private int age;

    @RequestMapping(value = "/miya")
    public String miya() {
        return name + ":" + age;
    }
}

        啟動程式SpringBoot,訪問:“http://localhost:8082/miya”:

        二。將配置檔案的屬性賦給實體類

          (1)定義配置檔案---application.yml

my:
   name: forezp
   age: 12
   number: ${random.int}
   uuid: ${random.uuid}
   max: ${random.int(10)}
   value: ${random.value}
   greeting: hi,i'm ${my.name}

        (2)建立JavaBean

package com.forezp.helloworld;

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

@ConfigurationProperties(prefix = "my")
@Component
public class ConfigBean {
    private int age;
    private String name;
    private int number;
    private String uuid;
    private int max;
    private String value;
    private String greeting;

    public int getAge() {
        return age;
    }

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


    public String getName() {
        return name;
    }

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

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }
}

         (3)建立Controller,讀取ConfigBean類的屬性[email protected]({ConfigBean.class})

package com.forezp.helloworld;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableConfigurationProperties({ConfigBean.class})
public class LucyController {
    @Autowired
    ConfigBean configBean;

    @RequestMapping(value = "/lucy")
    public String miya() {
        return configBean.getGreeting() + "-" + configBean.getName() + "-" +
                configBean.getUuid() + "-" + configBean.getMax();
    }
}

        啟動工程,訪問:“http://localhost:8082/lucy”:

      三。自定義配置檔案 

          1.自定義配置檔案--test.properties

com.forezp.name=forezp
com.forezp.age=12

          2. 建立JavaBean--User,讀取配置檔案屬性和屬性值:@Configuration、@PropertySource、@ConigurationProperties

package com.forezp.helloworld;

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

@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.forezp")
public class User {
    private String name;
    private int age;

    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;
    }
}

       3.建立一個類:LindaController,開啟RestController功能,同時指明需要引用的JavaBean的類,開啟引用配置屬性的功能

package com.forezp.helloworld;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableConfigurationProperties({ConfigBean.class, User.class})
public class LindaController {
    @Autowired
    ConfigBean configBean;

    @RequestMapping(value = "/linda")
    public String miya() {
        return configBean.getGreeting() + "-" + configBean.getName() + "-" +
                configBean.getUuid() + configBean.getMax();
    }

    @Autowired
    User user;

    @RequestMapping(value = "/user")
    public String user() {
        return user.getName() + ": " + user.getAge();
    }
}

       啟動工程,訪問:http://localhost:8082/linda

     四。多個環境的配置檔案 

        SpringBoot支援程式啟東時在配置檔案application.yml中指定環境的配置檔案,配置檔案的格式為application-{profile}.properties,其中{profile}對應環境標識。

             application-test.properties(yml)---測試環境

             application-dev.properties(yml)---開發環境

             application-prod.properties(yml)---生產環境

         1.配置檔案資訊為:application.yml

spring:
  profiles:
    active: dev

         2.application-dev.yml

server:
  port: 8082

         工程預設埠為8080,以上所有例子訪問8082是已經修改了環境,埠改變。 

結語:

        用自己的實力,索要自己的未來。