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. KK 与答辩

    KK 与答辩 解读一下题:如果在所有场的答辩中,有某个人的总分都要低于kk的总分,就说kk碾压该人 --> 如果在某场答辩中这个人的总分大于kk,那么就说明kk不能碾压该人. 思路就清晰了,我们 ...

  2. FreeSWITCH添加iLBC编码及转码

    操作系统 :CentOS 7.6_x64 FreeSWITCH版本 :1.10.9 一.安装ilbc库 从第三方库里下载指定版本: git clone https://freeswitch.org/s ...

  3. windows系统git使用ssh方式和gitee/github进行同步

    前言 在从github/gitee远程仓库获取代码时,除了使用https方式,我们还可以使用ssh连接的方式与远程仓库服务器通信,其好处是有时会比https更方便.稳定.快速. 和与普通的linux服 ...

  4. Mysql中的数据类型注意事项

    整型数据类型 MySQL数据类型 含义(有符号) tinyint 1字节,范围(-128~127) smallint 2字节,范围(-32768~32767) mediumint 3字节,范围(-83 ...

  5. 京东小程序接入ARVR的技术方案和性能调优

    作者:京东零售 戴旭 京东小程序是一个开放技术平台,正在被越来越多的头部品牌选择,用于站内私域流量的营销和运营.诸如各种日化.奢侈品等品牌对ARVR有较多的诉求,希望京东小程序引擎提供一些底层能力,叠 ...

  6. C# 控制系统任务栏的显示与隐藏

    [DllImport("user32.dll")] public static extern int FindWindow(string lpClassName, string l ...

  7. Godot 4.0 设置应用程序图标、项目图标

    godot版本:4.0.2,理论上4.0.0版也适用. 本文章是针对window应用程序而写的,其他平台不一定适用,仅供参考. 效果 输出的可执行文件图标为指定的图标,适配多种尺寸 执行时窗口图标为指 ...

  8. Redis篇一之基础数据结构

    文章目录 Redis的数据结构 String类型**** Hash类型 List类型 Set类型 SortedSet类型 BitMap类型 HyperLogLog 总结 Redis诞生于2009年全称 ...

  9. DFS(深度优先搜索) 总是需要重置 visited 的状态吗?

    问题来自 P1902 刺杀大使,在最初的实现中 DFS 中一段代码如下: visited[x2][y2] = true; flag = dfs(v, x2, y2); visited[x2][y2] ...

  10. AI 在 API 设计中的应用:如何利用 Al 快速实现 API 开发和测试

    一.引言 在当今互联网技术的快速发展中,API 成为了越来越多的软件和系统之间交互的核心方式,而 API 的质量和效率对于软件的开发和运维都至关重要.为了提高 API 的设计.开发.测试和运维的效率和 ...