欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot如何通過@Profile注解配置多環(huán)境

 更新時間:2023年06月13日 09:15:02   作者:fengyehongWorld  
在Spring中,可以使用配置文件的方式來指定不同環(huán)境下所需要的配置信息,本文給大家介紹SpringBoot如何通過@Profile注解配置多環(huán)境,感興趣的朋友跟隨小編一起看看吧

一. 使用場景

在Spring中,可以使用配置文件的方式來指定不同環(huán)境下所需要的配置信息

?application.yml

spring:
  profiles:
  	# 通過active來指定當(dāng)前所處的開發(fā)環(huán)境
    active: dev

?application-dev.yml

spring:
  datasource:
    url: jdbc:mysql://localhost/myblog-dev
    username: dev
    password: mysql
    driver-class-name: com.mysql.cj.jdbc.Driver

?application-product.yml

spring:
  datasource:
    url: jdbc:mysql://localhost/myblog-product
    username: product
    password: mysql
    driver-class-name: com.mysql.cj.jdbc.Driver

但有時候,我們不通過配置文件,而是通過配置類的方式來指定不同環(huán)境下的配置信息,
此時就需要用到@Profile注解。

二. 前期準(zhǔn)備

?用來封裝數(shù)據(jù)庫信息的Entity

import lombok.Builder;
import lombok.Data;
@Builder
@Data
public class DBInfoEntity {
    private String url;
    private String port;
    private String userName;
    private String password;
}

?配置接口

public interface Config {
	// 獲取數(shù)據(jù)庫信息
    DBInfoEntity getDBInfo();
	// 獲取系統(tǒng)URL
    String getSystemUrl();
}

三. @Profile注解作用于類上

  • 我們使用@Profile注解分別作用于如下所示的兩個配置類上,分別指定devproduct環(huán)境下才能起作用。
  • 我們通過@Configuration注解指定兩個配置類的Bean名稱都是MyConfig,一般情況下會報錯,因為Spring的IOC容器中,Bean的名稱是唯一的,但是我們使用了@Profile注解指定了開發(fā)環(huán)境,不滿足指定開發(fā)環(huán)境的配置類不會被添加到Bean中,所以不會報錯。

3.1 配置類

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration("MyConfig")
// 指定開發(fā)環(huán)境為dev
@Profile("dev")
public class MyConfig1 implements Config {
    @Override
    public DBInfoEntity getDBInfo() {
        return DBInfoEntity.builder()
                .url("https://127.0.0.1")
                .port("8080")
                .userName("devUser")
                .password("110120")
                .build();
    }
    @Override
    public String getSystemUrl() {
        return "https://www.dev.com";
    }
}
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration("MyConfig")
// 指定開發(fā)環(huán)境為product
@Profile("product")
public class MyConfig2 implements Config {
    @Override
    public DBInfoEntity getDBInfo() {
        return DBInfoEntity.builder()
                .url("https://127.0.0.2")
                .port("8089")
                .userName("prodUser")
                .password("999000")
                .build();
    }
    @Override
    public String getSystemUrl() {
        return "https://www.prod.com";
    }
}

3.2 效果

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class Test32Controller implements CommandLineRunner {
	// 注入接口,會自動從IOC容器中獲取該接口的實現(xiàn)類
    @Autowired
    private Config config;
    @Override
    public void run(String... args) throws Exception {
        DBInfoEntity dbInfo = config.getDBInfo();
        System.out.println(dbInfo);
        String systemUrl = config.getSystemUrl();
        System.out.println(systemUrl);
    }
}

??????dev環(huán)境

??????product環(huán)境

四. @Profile注解作用于方法上

4.1 定義一個生產(chǎn)環(huán)境的注解

當(dāng)@Profile注解作用于自定義注解上時,自定義注解便可標(biāo)識開發(fā)環(huán)境,相當(dāng)于是@Profile(“開發(fā)環(huán)境名稱”)的簡寫方式。

import org.springframework.context.annotation.Profile;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Profile("product")
public @interface ProductionAnnotation {
}

4.2 配置類

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class MyConfig3 {
	// 開發(fā)環(huán)境時,才會注入IOC容器
    @Bean
    @Profile("dev")
    public String getNameDev() {
        return "devName";
    }
	// 生產(chǎn)環(huán)境時,才會注入IOC容器
    @Bean
    @ProductionAnnotation  // 相當(dāng)于 @Profile("product")
    public String getNameProduct() {
        return "productName";
    }
}

4.3 效果

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Test32Controller implements CommandLineRunner {
    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public void run(String... args) throws Exception {
        // 判斷當(dāng)前IOC容器中是否存在名稱為 getNameDev 的Bean
        boolean getNameDevExist = applicationContext.containsBean("getNameDev");
        if (getNameDevExist) {
            // 從IOC容器中獲取出名稱為 getNameDev 的Bean
            Object bean1 = applicationContext.getBean("getNameDev");
            System.out.println("目前所在的是開發(fā)環(huán)境!");
            System.out.println(bean1);
        }
        boolean getNameProductExist = applicationContext.containsBean("getNameProduct");
        if (getNameProductExist) {
            Object bean2 = applicationContext.getBean("getNameProduct");
            System.out.println("目前所在的是生產(chǎn)環(huán)境!");
            System.out.println(bean2);
        }
    }
}

??????dev環(huán)境

??????product環(huán)境

參考資料

Springboot中的@Profile注解

到此這篇關(guān)于SpringBoot 通過@Profile注解配置多環(huán)境的文章就介紹到這了,更多相關(guān)SpringBoot 配置多環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 講解Java編程中finally語句的基本使用方法

    講解Java編程中finally語句的基本使用方法

    這篇文章主要介紹了講解Java編程中finally語句的基本使用方法,finally在異常處理中的使用時Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11
  • MybatisPlusException:Failed?to?process,Error?SQL異常報錯的解決辦法

    MybatisPlusException:Failed?to?process,Error?SQL異常報錯的解決辦法

    這篇文章主要給大家介紹了關(guān)于MybatisPlusException:Failed?to?process,Error?SQL異常報錯的解決辦法,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-03-03
  • Mybatis基于xml配置實現(xiàn)單表的增刪改查功能

    Mybatis基于xml配置實現(xiàn)單表的增刪改查功能

    這篇文章主要介紹了Mybatis基于xml配置實現(xiàn)單表的增刪改查,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • 如何解決@NotBlank不生效的問題

    如何解決@NotBlank不生效的問題

    這篇文章主要介紹了如何解決@NotBlank不生效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 深入了解Spring中的FactoryBean

    深入了解Spring中的FactoryBean

    這篇文章主要介紹了深入了解Spring中的FactoryBean,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • Java中流的有關(guān)知識點詳解

    Java中流的有關(guān)知識點詳解

    今天小編就為大家分享一篇關(guān)于Java中流的有關(guān)知識點詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java后臺通過Collections獲取list集合中最大數(shù),最小數(shù)代碼

    Java后臺通過Collections獲取list集合中最大數(shù),最小數(shù)代碼

    這篇文章主要介紹了Java后臺通過Collections獲取list集合中最大數(shù),最小數(shù)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • IntelliJ IDEA創(chuàng)建maven多模塊項目(圖文教程)

    IntelliJ IDEA創(chuàng)建maven多模塊項目(圖文教程)

    這篇文章主要介紹了IntelliJ IDEA創(chuàng)建maven多模塊項目(圖文教程),非常具有實用價值,需要的朋友可以參考下
    2017-09-09
  • java+Okhttp3調(diào)用接口的實例

    java+Okhttp3調(diào)用接口的實例

    這篇文章主要介紹了java+Okhttp3調(diào)用接口的實例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java實現(xiàn)分頁顯示效果

    java實現(xiàn)分頁顯示效果

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)頁顯示效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08

最新評論