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

SpringBoot中多環(huán)境配置和@Profile注解示例詳解

 更新時間:2023年01月19日 08:52:08   作者:彭世瑜  
這篇文章主要介紹了SpringBoot中多環(huán)境配置和@Profile注解,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、使用@Profile

1.1、@Profile修飾類

開發(fā)環(huán)境

package com.example.demo.config;

import com.example.demo.entity.AppData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("development")
public class DevelopmentConfig {
    @Bean
    public AppData getAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("development");

        return appData;
    }
}

正式環(huán)境

package com.example.demo.config;

import com.example.demo.entity.AppData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("production")
public class ProductionConfig {

    @Bean
    public AppData getAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("production");

        return appData;
    }
}

1.2、@Profile修飾方法

package com.example.demo.config;

import com.example.demo.entity.AppData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class AppConfig {
    // 開發(fā)環(huán)境
    @Bean("appConfigData")
    @Profile("development")
    public AppData getDevelopmentAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app development");

        return appData;
    }

    // 正式環(huán)境
    @Bean("appConfigData")
    @Profile("production")
    public AppData getProductionAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app production");

        return appData;
    }
}

1.3、@Profile修飾注解

1、定義注解

開發(fā)環(huán)境

package com.example.demo.annotation;

import org.springframework.context.annotation.Profile;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("development")
public @interface Development {
}

正式環(huán)境

package com.example.demo.annotation;

import org.springframework.context.annotation.Profile;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("production")
public @interface Production {
}

2、使用注解

package com.example.demo.config;

import com.example.demo.annotation.Development;
import com.example.demo.annotation.Production;
import com.example.demo.entity.AppData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    // 開發(fā)環(huán)境
    @Bean("appConfigData")
    @Development
    public AppData getDevelopmentAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app development");

        return appData;
    }

    // 正式環(huán)境
    @Bean("appConfigData")
    @Production
    public AppData getProductionAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app production");

        return appData;
    }
}

二、激活@Profile

2.1、配置文件方式激活@Profile

application.properties

spring.profiles.active=production 

application.yml

spring:
  profiles:
    active: production

2.2、命令行方式激活@Profile

java -jar target/demo-0.0.1-SNAPSHOT.jar  --spring.profiles.active=production

三、多Profile資源文件

配置文件

# 公共配置
application.properties

# development
application-development.properties

# production
application-production.properties

application.properties

# 激活配置文件
spring.profiles.active=production

application-development.properties

server.port=8081

application-production.properties

server.port=8082

完整代碼 https://github.com/mouday/spring-boot-demo/tree/master/SpringBoot-Profile

參考
Springboot中的@Profile注解

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

相關(guān)文章

  • spring security登錄成功后通過Principal獲取名返回空問題

    spring security登錄成功后通過Principal獲取名返回空問題

    這篇文章主要介紹了spring security登錄成功后通過Principal獲取名返回空問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 解決SpringBoot web項目啟動后立即關(guān)閉的問題

    解決SpringBoot web項目啟動后立即關(guān)閉的問題

    這篇文章主要介紹了解決SpringBoot web項目啟動后立即關(guān)閉的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java導(dǎo)出Excel統(tǒng)計報表合并單元格的方法詳解

    Java導(dǎo)出Excel統(tǒng)計報表合并單元格的方法詳解

    我們在日常編程過程中,總是會碰見導(dǎo)出相關(guān)表格信息的需求,所以就讓我們一起來學(xué)習(xí)一下,這篇文章主要給大家介紹了關(guān)于Java導(dǎo)出Excel統(tǒng)計報表合并單元格的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • 深入理解Java中的Properties類

    深入理解Java中的Properties類

    Properties類是Java中用于處理配置文件的工具類,它繼承自 Hashtable類,實(shí)現(xiàn)了Map接口,本文主要介紹了Java中的Properties類,感興趣的可以了解一下
    2024-01-01
  • Java List Object[]轉(zhuǎn)換成List T的實(shí)例

    Java List Object[]轉(zhuǎn)換成List T的實(shí)例

    這篇文章主要介紹了Java List Object[]轉(zhuǎn)換成List T的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • mybatis plus in方法使用詳解

    mybatis plus in方法使用詳解

    這篇文章主要介紹了mybatis plus in方法使用詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • 手把手教你如何利用SpringBoot實(shí)現(xiàn)審核功能

    手把手教你如何利用SpringBoot實(shí)現(xiàn)審核功能

    審核功能經(jīng)過幾個小時的奮戰(zhàn)終于完成了,現(xiàn)在我就與廣大網(wǎng)友分享我的成果,這篇文章主要給大家介紹了關(guān)于如何利用SpringBoot實(shí)現(xiàn)審核功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • SpringBoot+Vue解決跨域問題幾種處理方案介紹

    SpringBoot+Vue解決跨域問題幾種處理方案介紹

    同源策略是一種約定,它是瀏覽器最核心也最基本的安全功能,同源策略會阻止一個域的javascript腳本和另外一個域的內(nèi)容進(jìn)行交互,所以本文我家里聊聊SpringBoot結(jié)合Vue解決跨域問題幾種處理方案吧
    2025-07-07
  • Go Java算法之解碼方法示例詳解

    Go Java算法之解碼方法示例詳解

    這篇文章主要為大家介紹了Go Java算法之解碼方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 基于java中BlockingQueue的使用介紹

    基于java中BlockingQueue的使用介紹

    本篇文章小編為大家介紹,基于java中BlockingQueue的使用介紹。需要的朋友參考下
    2013-04-04

最新評論