国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

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

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot 通過@Profile注解配置多環(huán)境。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

參考資料

  1. Springboot中的@Profile注解


一. 使用場(chǎng)景

在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

但有時(shí)候,我們不通過配置文件,而是通過配置類的方式來指定不同環(huán)境下的配置信息,
此時(shí)就需要用到@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注解分別作用于如下所示的兩個(gè)配置類上,分別指定devproduct環(huán)境下才能起作用。
  • 我們通過@Configuration注解指定兩個(gè)配置類的Bean名稱都是MyConfig,一般情況下會(huì)報(bào)錯(cuò),因?yàn)镾pring的IOC容器中,Bean的名稱是唯一的,但是我們使用了@Profile注解指定了開發(fā)環(huán)境,不滿足指定開發(fā)環(huán)境的配置類不會(huì)被添加到Bean中,所以不會(huì)報(bào)錯(cuò)。

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 {
	
	// 注入接口,會(huì)自動(dòng)從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)境

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

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

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


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

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

  • 當(dāng)@Profile注解作用于自定義注解上時(shí),自定義注解便可標(biāo)識(shí)開發(fā)環(huán)境,相當(dāng)于是@Profile(“開發(fā)環(huán)境名稱”)的簡(jiǎ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)境時(shí),才會(huì)注入IOC容器
    @Bean
    @Profile("dev")
    public String getNameDev() {
        return "devName";
    }
	
	// 生產(chǎn)環(huán)境時(shí),才會(huì)注入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)境

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

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

SpringBoot 通過@Profile注解配置多環(huán)境文章來源地址http://www.zghlxwxcb.cn/news/detail-491192.html

到了這里,關(guān)于SpringBoot 通過@Profile注解配置多環(huán)境的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • linux profile文件環(huán)境變量配置

    linux profile文件環(huán)境變量配置

    profile 文件位于/etc/目錄下 /etc/profile , 當(dāng)?shù)侨胂到y(tǒng)時(shí)候獲得一個(gè) shell 進(jìn)程時(shí),其讀取環(huán)境profile 文件時(shí)候會(huì)讀取,/etc/bash.bashrc,/etc/profile.d 文件下配置的sh文件,所以我們也可以在profile.d 和bash.bashrc 目錄下創(chuàng)建sh文件,配置環(huán)境變量 profile,路徑:/etc/profile,用于設(shè)置系統(tǒng)級(jí)

    2024年02月05日
    瀏覽(28)
  • 【maven】通過profiles實(shí)現(xiàn):怎樣激活某個(gè)倉庫、同時(shí)加載多個(gè)profile、不同環(huán)境加載不同依賴jar

    【maven】通過profiles實(shí)現(xiàn):怎樣激活某個(gè)倉庫、同時(shí)加載多個(gè)profile、不同環(huán)境加載不同依賴jar

    Maven中的profile是一組可選的配置,可以用來設(shè)置或者覆蓋配置默認(rèn)值。有了profile,你就可以為不同的環(huán)境定制構(gòu)建。 profile可以在pom.xml中和maven的setting.xml文件中配置,如下: 在上述示例中,我們定義了一個(gè)名為\\\"nexus\\\"的profile,并在其中設(shè)置了一個(gè)名為\\\"my-repo\\\"的Maven倉庫。該倉

    2024年02月13日
    瀏覽(16)
  • Mac環(huán)境配置(Java)----使用bash_profile進(jìn)行配置

    Mac環(huán)境配置(Java)----使用bash_profile進(jìn)行配置

    目錄 ?一、下載Java 1、官方地址: 2、網(wǎng)盤下載地址(Java8): 二、Java環(huán)境配置(以Java8配置為例,所有版本配置方法一樣) 1、打開軟件--終端 2、首先查看本機(jī)Java的安裝地址(系統(tǒng)默認(rèn)的) 3、查看到Java8安裝的路徑如下:? 4、如果是第一次配置環(huán)境變量,使用命令:【t

    2024年02月16日
    瀏覽(23)
  • mac 配置環(huán)境變量 .bash_profile文件

    打開.bash_profile文件 open -e ~/.bash_profile 編輯:增減自己的各軟件配置 export M2_HOME=/Users/lizhen/apache-maven-3.5.4 export PATH=$PATH:$M2_HOME/bin PATH=$PATH:/usr/local/mysql/bin # JAVA JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-18.0.2.jdk/Contents/Home # Android SDK export ANDROID_HOME=/Users/lizhen/Library/Android/sdk export PATH

    2024年02月06日
    瀏覽(19)
  • Spring Profile與PropertyPlaceholderConfigurer實(shí)現(xiàn)項(xiàng)目多環(huán)境配置切換

    Spring Profile與PropertyPlaceholderConfigurer實(shí)現(xiàn)項(xiàng)目多環(huán)境配置切換

    最近考慮項(xiàng)目在不同環(huán)境下配置的切換,使用profile注解搭配PropertyPlaceholderConfigurer實(shí)現(xiàn)對(duì)配置文件的切換,簡(jiǎn)單寫了個(gè)demo記錄下實(shí)現(xiàn)。 @Profile @Profile 通過對(duì)bean進(jìn)行修飾,來限定spring在bean管理時(shí)的初始化情況,只有環(huán)境中激活的profile狀態(tài)和修飾的value值對(duì)應(yīng)時(shí),該bean才會(huì)被

    2024年02月12日
    瀏覽(20)
  • MacOS環(huán)境配置 .zshrc .bashrc .bash_profile

    MacOS環(huán)境配置 .zshrc .bashrc .bash_profile

    每當(dāng)學(xué)習(xí)一門新技術(shù)的時(shí)候,其中一個(gè)最大的攔路虎就是環(huán)境安裝配置,比如java,安卓,比如php都需要安裝和配置一大堆工具,安裝不順利的時(shí)候無疑會(huì)在我們的熱情上澆一頭冷水。這不,最近打算學(xué)習(xí)Flutter開發(fā),光是安裝配置就搗鼓了好幾天,現(xiàn)在記錄下來Mac環(huán)境變量的

    2023年04月24日
    瀏覽(23)
  • springboot之一:配置文件(內(nèi)外部配置優(yōu)先順序+properties、xml、yaml基礎(chǔ)語法+profile動(dòng)態(tài)切換配置、激活方式)

    springboot之一:配置文件(內(nèi)外部配置優(yōu)先順序+properties、xml、yaml基礎(chǔ)語法+profile動(dòng)態(tài)切換配置、激活方式)

    Spring Boot是基于約定的,所以很多配置都有默認(rèn)值,但如果想使用自己的配置替換默認(rèn)配置的話,就可以使用application.properties或者application.yml(application.yaml)進(jìn)行配置。 注意配置文件的命名必須是application開頭。 在同一級(jí)目錄下優(yōu)先級(jí)為:properties yml yaml file:../config/ :當(dāng)前項(xiàng)目

    2024年02月10日
    瀏覽(11)
  • Spring @Profile注解使用和源碼解析

    Spring @Profile注解使用和源碼解析

    在之前的文章中,寫了一篇使用Spring @Profile實(shí)現(xiàn)開發(fā)環(huán)境,測(cè)試環(huán)境,生產(chǎn)環(huán)境的切換,之前的文章是使用SpringBoot項(xiàng)目搭建,實(shí)現(xiàn)了不同環(huán)境數(shù)據(jù)源的切換,在我們實(shí)際開發(fā)中,會(huì)分為dev,test,prod等環(huán)境,他們之間數(shù)獨(dú)立的,今天進(jìn)來詳解介紹Spring @Profile的原理。 # Spring注

    2023年04月13日
    瀏覽(19)
  • SpringBoot通過@Cacheable注解實(shí)現(xiàn)緩存功能

    SpringBoot通過@Cacheable注解實(shí)現(xiàn)緩存功能

    Spring 從 3.1 開始就引入了對(duì) Cache 的支持。定義了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口來統(tǒng)一不同的緩存技術(shù)。并支持使用 JCache(JSR-107) 注解簡(jiǎn)化我們的開發(fā)。 其使用方法和原理都類似于 Spring 對(duì)事務(wù)管理的支持,Spring Cache 是作用在方法上的,其核

    2024年02月10日
    瀏覽(25)
  • .Net Core項(xiàng)目在linux部署實(shí)戰(zhàn) 1.sdk下載 2.環(huán)境變量配置/ect/profile 3.運(yùn)行

    .Net Core項(xiàng)目在linux部署實(shí)戰(zhàn) 1.sdk下載 2.環(huán)境變量配置/ect/profile 3.運(yùn)行

    1)下載.net core sdk https://download.visualstudio.microsoft.com/download/pr/01292c7c-a1ec-4957-90fc-3f6a2a1e5edc/025e84c4d9bd4aeb003d4f07b42e9159/dotnet-sdk-6.0.418-linux-x64.tar.gz 2)配置下環(huán)境變量? step1: // 解壓到指定目錄 ?我們可以看出來,$HOME就是root? step2: // 通過vim或者終端配置下環(huán)境變量,這樣子是永久生

    2024年01月17日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包