SpringBoot Profiles

關(guān)聯(lián)文章:SpringBoot 參數(shù)映射,開發(fā)及線上設(shè)置

本文目類:

  1. 根據(jù)環(huán)境更改配置,不同環(huán)境配置不同
    1.1 YAML 更改配置
    1.2 application-${profile}.properties 更改配置
  2. @Profile 使用
  3. 工具類

根據(jù)環(huán)境更改配置

官方文檔

方式一 YAML

新建一個web項(xiàng)目,新建配置文件application.yml:

server:
  port: 9090

spring:
  profiles:
    active: pro
---
#development environment
spring:
  profiles: dev

test_name: dev_testName
---
#production environment
spring:
  profiles: pro

test_name: pro_testName

application.yml文件分為三部分,使用 --- 來作為分隔符,第一部分通用配置部分,表示兩個環(huán)境都通用的屬性, 后面三段分別為:開發(fā),生產(chǎn)
profiles.active后面的文字決定 開發(fā)模式,還是生成模式

創(chuàng)建TestController:

@RestController
public class HelloController {

    @Value("${test_name}")
    private String testName;

    @GetMapping("/hello")
    public String hello() {
        return testName;
    }
}

啟動項(xiàng)目,訪問http://127.0.0.1:9090/hello

image.png

修改application.yml 文件

server:
  port: 9090

spring:
  profiles:
    active: dev
---
#development environment
spring:
  profiles: dev

test_name: dev_testName
---
#production environment
spring:
  profiles: pro

test_name: pro_testName

修改 active: dev
啟動項(xiàng)目,訪問http://127.0.0.1:9090/hello

image.png

結(jié)論:配置profile.active不同,獲取的test_name的值不同。

方式二:application-${profile}.properties 更改配置

you can use application-${profile}.properties to specify profile-specific values.

resource 目錄下創(chuàng)建文件application-dev.yml,application-pro.yml文件


image.png

內(nèi)容分別為:

test_name: dev_testName_file
test_name: pro_testName_file

application.yml 內(nèi)容為:

server:
  port: 9090

spring:
  profiles:
    active: dev

啟動項(xiàng)目測試,和上面結(jié)果一樣,不同配置,讀取的配置不同。

@Profile 使用

官方地址

  1. @Profile注解使用范圍:
    @Configration 和 @Component 注解的類及其方法,其中包括繼承了@Component的注解:@Service、@Controller、@Repository等…

  2. @Profile可接受一個或者多個參數(shù)

@Service
@Profile({"pro","dev"})

Demo

創(chuàng)建接口類

public interface HelloService {
    String getTestName();
}

創(chuàng)建兩個實(shí)現(xiàn)類:

@Service
@Profile("dev")
public class DevHelloServiceImpl implements HelloService {

    @Value("${test_name}")
    private String testName;

    @Override
    public String getTestName() {
        return "開發(fā)環(huán)境   "+testName;
    }
}
@Service
@Profile("pro")
public class ProHelloServiceImpl implements HelloService {
    @Value("${test_name}")
    private String testName;

    @Override
    public String getTestName() {
        return "生產(chǎn)環(huán)境   " + testName;
    }
}

@Profile("pro") 注解代表不同的環(huán)境配置。

編寫接口測試:

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/")
    public String getTestName() {
        return helloService.getTestName();
    }
}

當(dāng)前application.yml文件中 profiles.active: dev
啟動項(xiàng)目,訪問:http://127.0.0.1:9090/
返回如下:

image.png

工具類

@Component
public class ProfileUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    /**
     * 獲取當(dāng)前環(huán)境的active profile.
     *
     * @return
     */
    public static String getActiveProfile() {
        String[] activeProfiles = context.getEnvironment().getActiveProfiles();
        if (!ArrayUtils.isEmpty(activeProfiles)) {
            return activeProfiles[0];
        }
        return "";
    }
}

源碼地址:https://github.com/lbshold/springboot/tree/master/Spring-Boot-ProfileDemo

參考文章:
https://blog.csdn.net/zknxx/article/details/77906096
https://blog.csdn.net/fmuma/article/details/82787500

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容