最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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,一般情況下會報錯,因?yàn)镾pring的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容器中獲取該接口的實(shí)現(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)文章

  • 詳解SpringBoot中文件上傳大小限制問題的解決方案

    詳解SpringBoot中文件上傳大小限制問題的解決方案

    在開發(fā)Web應(yīng)用程序時,文件上傳是一個常見的需求,本文將詳細(xì)介紹如何分析和解決Spring Boot文件上傳大小限制問題,并提供多種配置方式,希望對大家有所幫助
    2025-07-07
  • mybatis-plus讀取JSON類型的方法實(shí)現(xiàn)

    mybatis-plus讀取JSON類型的方法實(shí)現(xiàn)

    這篇文章主要介紹了mybatis-plus讀取JSON類型的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 詳解使用spring validation完成數(shù)據(jù)后端校驗(yàn)

    詳解使用spring validation完成數(shù)據(jù)后端校驗(yàn)

    這篇文章主要介紹了詳解使用spring validation完成數(shù)據(jù)后端校驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java在Linux下 不能處理圖形的解決辦法 分享

    Java在Linux下 不能處理圖形的解決辦法 分享

    Java在Linux下 不能處理圖形的解決辦法 分享,需要的朋友可以參考一下
    2013-06-06
  • eclipse 如何創(chuàng)建 user library 方法詳解

    eclipse 如何創(chuàng)建 user library 方法詳解

    這篇文章主要介紹了eclipse 如何創(chuàng)建 user library 方法詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • JavaWeb response和request對象原理及實(shí)例解析

    JavaWeb response和request對象原理及實(shí)例解析

    這篇文章主要介紹了JavaWeb response和request對象原理及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • JavaWeb中Servlet的生命周期及線程安全問題詳解

    JavaWeb中Servlet的生命周期及線程安全問題詳解

    這篇文章主要介紹了JavaWeb中Servlet的生命周期及線程安全問題詳解,Servlet?生命周期可被定義為從創(chuàng)建直到毀滅的整個過程,Servlet體系結(jié)構(gòu)是建立在Java多線程機(jī)制之上的,它的生命周期是由Web容器負(fù)責(zé)的,需要的朋友可以參考下
    2024-01-01
  • Spring Bean管理注解方式代碼實(shí)例

    Spring Bean管理注解方式代碼實(shí)例

    這篇文章主要介紹了Spring Bean管理注解方式代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • java final本質(zhì)詳解

    java final本質(zhì)詳解

    在本篇文章里小編給大家分享的是關(guān)于java final本質(zhì)的相關(guān)知識點(diǎn)內(nèi)容,有需要的朋友們可以參考下。
    2019-09-09
  • 一文詳解Java抽象類到底有多抽象

    一文詳解Java抽象類到底有多抽象

    這篇文章主要介紹了一文詳解Java抽象類到底有多抽象,抽象方法所在的類必須是抽象類,子類若繼承了一個抽象類,就必須覆寫父類的所有抽象方法,這里的子類是普通類,是強(qiáng)制要求覆寫所有抽象方法,但是如果子類也是一個抽象類,那么就可以不覆寫
    2022-06-06

最新評論

江城| 楚雄市| 泰和县| 县级市| 枞阳县| 宁阳县| 丰顺县| 南雄市| 镇平县| 大城县| 吴忠市| 汝南县| 武汉市| 汪清县| 海晏县| 佛学| 玉树县| 平江县| 井研县| 漳州市| 塔城市| 来凤县| 洛隆县| 沁源县| 防城港市| 太仆寺旗| 故城县| 财经| 荥经县| 普兰店市| 大姚县| 灌云县| 海兴县| 彭山县| 资源县| 西平县| 大荔县| 梁山县| 甘谷县| 柞水县| 麻阳|