SpringBoot中多环境配置和@Profile注解示例详解

 更新时间:2023年01月19日 08:52:08   作者:彭世瑜  
这篇文章主要介绍了SpringBoot中多环境配置和@Profile注解,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、使用@Profile

1.1、@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
@Profile("development")
public class DevelopmentConfig {
    @Bean
    public AppData getAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("development");

        return appData;
    }
}

正式环境

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 {
    // 开发环境
    @Bean("appConfigData")
    @Profile("development")
    public AppData getDevelopmentAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app development");

        return appData;
    }

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

        return appData;
    }
}

1.3、@Profile修饰注解

1、定义注解

开发环境

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 {
}

正式环境

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 {
    // 开发环境
    @Bean("appConfigData")
    @Development
    public AppData getDevelopmentAppData() {
        AppData appData = new AppData();
        appData.setEnvironmentName("app development");

        return appData;
    }

    // 正式环境
    @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注解

到此这篇关于SpringBoot中多环境配置和@Profile注解的文章就介绍到这了,更多相关SpringBoot 多环境配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • spring security登录成功后通过Principal获取名返回空问题

    spring security登录成功后通过Principal获取名返回空问题

    这篇文章主要介绍了spring security登录成功后通过Principal获取名返回空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • 解决SpringBoot web项目启动后立即关闭的问题

    解决SpringBoot web项目启动后立即关闭的问题

    这篇文章主要介绍了解决SpringBoot web项目启动后立即关闭的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java导出Excel统计报表合并单元格的方法详解

    Java导出Excel统计报表合并单元格的方法详解

    我们在日常编程过程中,总是会碰见导出相关表格信息的需求,所以就让我们一起来学习一下,这篇文章主要给大家介绍了关于Java导出Excel统计报表合并单元格的相关资料,需要的朋友可以参考下
    2021-10-10
  • 深入理解Java中的Properties类

    深入理解Java中的Properties类

    Properties类是Java中用于处理配置文件的工具类,它继承自 Hashtable类,实现了Map接口,本文主要介绍了Java中的Properties类,感兴趣的可以了解一下
    2024-01-01
  • Java List Object[]转换成List T的实例

    Java List Object[]转换成List T的实例

    这篇文章主要介绍了Java List Object[]转换成List T的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • mybatis plus in方法使用详解

    mybatis plus in方法使用详解

    这篇文章主要介绍了mybatis plus in方法使用详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • 手把手教你如何利用SpringBoot实现审核功能

    手把手教你如何利用SpringBoot实现审核功能

    审核功能经过几个小时的奋战终于完成了,现在我就与广大网友分享我的成果,这篇文章主要给大家介绍了关于如何利用SpringBoot实现审核功能的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • SpringBoot+Vue解决跨域问题几种处理方案介绍

    SpringBoot+Vue解决跨域问题几种处理方案介绍

    同源策略是一种约定,它是浏览器最核心也最基本的安全功能,同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互,所以本文我家里聊聊SpringBoot结合Vue解决跨域问题几种处理方案吧
    2025-07-07
  • Go Java算法之解码方法示例详解

    Go Java算法之解码方法示例详解

    这篇文章主要为大家介绍了Go Java算法之解码方法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • 基于java中BlockingQueue的使用介绍

    基于java中BlockingQueue的使用介绍

    本篇文章小编为大家介绍,基于java中BlockingQueue的使用介绍。需要的朋友参考下
    2013-04-04

最新评论