profile

使用说明:

@profile注解的作用是指定类或方法在特定的 Profile 环境生效,任何@Component或@Configuration注解的类都可以使用@Profile注解。

在使用DI来依赖注入的时候,能够根据@profile标明的环境,将注入符合当前运行环境的相应的bean。

使用要求

  1. @Component或@Configuration注解的类可以使用@profile

  2. @Profile中需要指定一个字符串,约定生效的环境

@Profile的使用位置

@Prifile修饰类

DevProfile.java

@Configuration
@Profile("dev")
public class DevProfile {
private static final Logger log = LoggerFactory.getLogger(DevProfile.class); @PostConstruct
public void init(){
log.info("-----this profile is dev-----");
}
}

TestProfile

@Configuration
@Profile("test")
public class TestProfile {
private static final Logger log = LoggerFactory.getLogger(TestProfile.class); @PostConstruct
public void init(){
log.info("-----this profile is test-----");
}
}

通过指定profile的值启动即可;

测试结果

dev

test

@Profile修饰方法

@Configuration
public class TestProfile {
private static final Logger log = LoggerFactory.getLogger(TestProfile.class); @Profile("test")
@Bean
public ProfileDto getTest(){
return new ProfileDto("test");
} @Profile("dev")
@Bean
public ProfileDto getDev(){
return new ProfileDto("dev");
}
}

PageController.java

@RequestMapping("/")
@RestController
public class PageController { @Resource
private ProfileDto profileDto; @GetMapping("/profileTest")
public ProfileDto profileTest(){
return profileDto;
}
}

测试结果

访问:http://localhost:8082/profileTest

dev

{"env":"dev"}

test

{"env":"test"}

@Profile修饰注解

@Profile注解支持定义在其他注解之上,以创建自定义场景注解。这样就创建了一个@Dev注解,

该注解可以标识bean使用于@Dev这个场景。后续就不再需要使用@Profile("dev")的方式,这样即可以简化代码。

注解 @DevEnv

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Profile("dev")
public @interface DevEnv {
}

注解 @TestEnv

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Profile("dev")
public @interface TestEnv {
}
@Configuration
public class TestProfile {
private static final Logger log = LoggerFactory.getLogger(TestProfile.class); @TestEnv
@Bean
public ProfileDto getTest() {
return new ProfileDto("test");
} @DevEnv
@Bean
public ProfileDto getDev() {
return new ProfileDto("dev");
}
}

测试方法跟结果跟修饰方法一致,不在赘述。

激活 Profile

实际使用中,注解中标示了prod、test、dev等多个环境,

运行时使用哪个profile由spring.profiles.active控制,以下说明2种方式 :配置文件方式、命令行方式。

配置文件方式

application.properties

spring.profiles.active=dev

application.yml

spring:
profiles:
active: dev

命令行方式

在打包后运行的时候,添加参数.

java -jar deme-profile-SNAPSHOT.jar   --spring.profiles.active=dev

【SpringBoot】条件装配 @profile的更多相关文章

  1. springboot自动装配(3)---条件注解@Conditional

    之前有说到springboot自动装配的时候,都是去寻找一个XXXAutoConfiguration的配置类,然而我们的springboot的spring.factories文件中有各种组件的自动装配 ...

  2. springBoot按条件装配:Condition

    编码格式转换器接口 package com.boot.condition.bootconditionconfig.converter; /** * 编码格式转换器接口 */ public interf ...

  3. 一步步从Spring Framework装配掌握SpringBoot自动装配

    目录 Spring Framework模式注解 Spring Framework@Enable模块装配 Spring Framework条件装配 SpringBoot 自动装配 本章总结 Spring ...

  4. Spring Boot之从Spring Framework装配掌握SpringBoot自动装配

    Spring Framework模式注解 模式注解是一种用于声明在应用中扮演“组件”角色的注解.如 Spring Framework 中的 @Repository 标注在任何类上 ,用于扮演仓储角色的 ...

  5. springboot自动装配

    Spring Boot自动配置原理 springboot自动装配 springboot配置文件 Spring Boot的出现,得益于“习惯优于配置”的理念,没有繁琐的配置.难以集成的内容(大多数流行第 ...

  6. spring的条件装配bean

    1 应用程序环境的迁移 问题: 开发软件时,有一个很大的挑战,就是将应用程序从一个环境迁移到另一个环境. 例如,开发环境中很多方式的处理并不适合生产环境,迁移后需要修改,这个过程可能会莫名的出现很多b ...

  7. SpringBoot启动流程分析(五):SpringBoot自动装配原理实现

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  8. Spring Framework 条件装配 之 @Conditional

    Spring Framework 条件装配 之 @Conditional 前言 了解SpringBoot的小伙伴对Conditional注解一定不会陌生,在SpringBoot项目中,Conditio ...

  9. SpringBoot自动装配原理解析

    本文包含:SpringBoot的自动配置原理及如何自定义SpringBootStar等 我们知道,在使用SpringBoot的时候,我们只需要如下方式即可直接启动一个Web程序: @SpringBoo ...

  10. 【Springboot】Springboot自动装配原理

    1.核心注解就是 EnableAutoConfiguration  该注解会激活SpringBoot的自动装配功能: 代码如下: @Target(ElementType.TYPE) @Retentio ...

随机推荐

  1. 【深度学习】【图像分类网络】(二)VisionTransformer

    Transformer简介 ![1png](file:///D:/资料/学习笔记/深度学习/图像分类/transformer/Self-Attention以及Multi-Head Attention/ ...

  2. DeepSpeed Chat: 一键式RLHF训练,让你的类ChatGPT千亿大模型提速省钱15倍

    DeepSpeed Chat: 一键式RLHF训练,让你的类ChatGPT千亿大模型提速省钱15倍 1. 概述 近日来,ChatGPT及类似模型引发了人工智能(AI)领域的一场风潮. 这场风潮对数字世 ...

  3. python+folium

    建模时无意中发现了一个很好用的交互式画地图的库!!

  4. 前端获取不到环境变量NODE_ENV

    有时候我们期望通过执行不同的 npm script 来区分诸如 dev.prod.uat.sit等多环境下使用的不同变量 今天我也在整环境变量,碰到一个小小的bug.装了 cross-env 但还是没 ...

  5. This application failed to start because it could not find or load the Qt platforms plugins

     由于一直在linux下操作,今天Qt移植平台的时候导致.exe可执行文件一直运行不起来,提示缺少某些dll库,这个问题解决起来简单(直接去qt源码里面查找对应库添加到可执行文件目录就行),但是之后一 ...

  6. YOLO1论文中文版

    文章目录 YOLO1中文版 摘要 1. 引言 2. 统一检测 2.1 网络设计 2.2 训练 2.3 推断 2.4 YOLO的限制 3. 与其它检测系统的比较 4. 实验 4. 1 与其它实时系统的比 ...

  7. Python-​​pprint的简单使用

    ​​Data pretty printer 一.简介​ ​​print()​和​​pprint()​都是python的打印模块,功能基本一样,唯一的区别就是​​pprint()​模块打印出来的数据结构 ...

  8. 2022-04-22:给你两个正整数数组 nums 和 target ,两个数组长度相等。 在一次操作中,你可以选择两个 不同 的下标 i 和 j , 其中 0 <= i, j < nums.leng

    2022-04-22:给你两个正整数数组 nums 和 target ,两个数组长度相等. 在一次操作中,你可以选择两个 不同 的下标 i 和 j , 其中 0 <= i, j < num ...

  9. 2022-09-15:Range模块是跟踪数字范围的模块。 设计一个数据结构来跟踪表示为 半开区间 的范围并查询它们。 半开区间 [left, right) 表示所有 left <= x < righ

    2022-09-15:Range模块是跟踪数字范围的模块. 设计一个数据结构来跟踪表示为 半开区间 的范围并查询它们. 半开区间 [left, right) 表示所有 left <= x < ...

  10. 2022-04-29:厨房里总共有 n 个橘子,你决定每一天选择如下方式之一吃这些橘子: 吃掉一个橘子。 如果剩余橘子数 n 能被 2 整除,那么你可以吃掉 n/2 个橘子。 如果剩余橘子数 n 能被

    2022-04-29:厨房里总共有 n 个橘子,你决定每一天选择如下方式之一吃这些橘子: 吃掉一个橘子. 如果剩余橘子数 n 能被 2 整除,那么你可以吃掉 n/2 个橘子. 如果剩余橘子数 n 能被 ...