SpringBoot如何通过@Profile注解配置多环境

 更新时间:2023年06月13日 09:15:02   作者:fengyehongWorld  
在Spring中,可以使用配置文件的方式来指定不同环境下所需要的配置信息,本文给大家介绍SpringBoot如何通过@Profile注解配置多环境,感兴趣的朋友跟随小编一起看看吧

一. 使用场景

在Spring中,可以使用配置文件的方式来指定不同环境下所需要的配置信息

⏹application.yml

spring:
  profiles:
  	# 通过active来指定当前所处的开发环境
    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

但有时候,我们不通过配置文件,而是通过配置类的方式来指定不同环境下的配置信息,
此时就需要用到@Profile注解

二. 前期准备

⏹用来封装数据库信息的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 {
	// 获取数据库信息
    DBInfoEntity getDBInfo();
	// 获取系统URL
    String getSystemUrl();
}

三. @Profile注解作用于类上

  • 我们使用@Profile注解分别作用于如下所示的两个配置类上,分别指定devproduct环境下才能起作用。
  • 我们通过@Configuration注解指定两个配置类的Bean名称都是MyConfig,一般情况下会报错,因为Spring的IOC容器中,Bean的名称是唯一的,但是我们使用了@Profile注解指定了开发环境,不满足指定开发环境的配置类不会被添加到Bean中,所以不会报错。

3.1 配置类

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration("MyConfig")
// 指定开发环境为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")
// 指定开发环境为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容器中获取该接口的实现类
    @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环境

💪💪💪product环境

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

4.1 定义一个生产环境的注解

@Profile注解作用于自定义注解上时,自定义注解便可标识开发环境,相当于是@Profile(“开发环境名称”)的简写方式。

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 {
	// 开发环境时,才会注入IOC容器
    @Bean
    @Profile("dev")
    public String getNameDev() {
        return "devName";
    }
	// 生产环境时,才会注入IOC容器
    @Bean
    @ProductionAnnotation  // 相当于 @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 {
        // 判断当前IOC容器中是否存在名称为 getNameDev 的Bean
        boolean getNameDevExist = applicationContext.containsBean("getNameDev");
        if (getNameDevExist) {
            // 从IOC容器中获取出名称为 getNameDev 的Bean
            Object bean1 = applicationContext.getBean("getNameDev");
            System.out.println("目前所在的是开发环境!");
            System.out.println(bean1);
        }
        boolean getNameProductExist = applicationContext.containsBean("getNameProduct");
        if (getNameProductExist) {
            Object bean2 = applicationContext.getBean("getNameProduct");
            System.out.println("目前所在的是生产环境!");
            System.out.println(bean2);
        }
    }
}

💪💪💪dev环境

💪💪💪product环境

参考资料

Springboot中的@Profile注解

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

相关文章

  • 详解SpringBoot中文件上传大小限制问题的解决方案

    详解SpringBoot中文件上传大小限制问题的解决方案

    在开发Web应用程序时,文件上传是一个常见的需求,本文将详细介绍如何分析和解决Spring Boot文件上传大小限制问题,并提供多种配置方式,希望对大家有所帮助
    2025-07-07
  • mybatis-plus读取JSON类型的方法实现

    mybatis-plus读取JSON类型的方法实现

    这篇文章主要介绍了mybatis-plus读取JSON类型的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • 详解使用spring validation完成数据后端校验

    详解使用spring validation完成数据后端校验

    这篇文章主要介绍了详解使用spring validation完成数据后端校验,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • Java在Linux下 不能处理图形的解决办法 分享

    Java在Linux下 不能处理图形的解决办法 分享

    Java在Linux下 不能处理图形的解决办法 分享,需要的朋友可以参考一下
    2013-06-06
  • eclipse 如何创建 user library 方法详解

    eclipse 如何创建 user library 方法详解

    这篇文章主要介绍了eclipse 如何创建 user library 方法详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • JavaWeb response和request对象原理及实例解析

    JavaWeb response和request对象原理及实例解析

    这篇文章主要介绍了JavaWeb response和request对象原理及实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • JavaWeb中Servlet的生命周期及线程安全问题详解

    JavaWeb中Servlet的生命周期及线程安全问题详解

    这篇文章主要介绍了JavaWeb中Servlet的生命周期及线程安全问题详解,Servlet 生命周期可被定义为从创建直到毁灭的整个过程,Servlet体系结构是建立在Java多线程机制之上的,它的生命周期是由Web容器负责的,需要的朋友可以参考下
    2024-01-01
  • Spring Bean管理注解方式代码实例

    Spring Bean管理注解方式代码实例

    这篇文章主要介绍了Spring Bean管理注解方式代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • java final本质详解

    java final本质详解

    在本篇文章里小编给大家分享的是关于java final本质的相关知识点内容,有需要的朋友们可以参考下。
    2019-09-09
  • 一文详解Java抽象类到底有多抽象

    一文详解Java抽象类到底有多抽象

    这篇文章主要介绍了一文详解Java抽象类到底有多抽象,抽象方法所在的类必须是抽象类,子类若继承了一个抽象类,就必须覆写父类的所有抽象方法,这里的子类是普通类,是强制要求覆写所有抽象方法,但是如果子类也是一个抽象类,那么就可以不覆写
    2022-06-06

最新评论