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. python移动同名文件

    import os import shutil def split_name(file): file_name, _ = file.split('.') return file_name def mo ...

  2. 基础算法(排序 & 查找)

    快速排序.归并排序.整数二分查找.浮点数二分查找 快速排序 主要思想是分治: 确定分界点 调整范围 递归处理左右两段 代码: #include <iostream> using names ...

  3. 学习C语言的第一天

    今天学习C语言学习了三个部分: 第一个部分是软件环境的搭建,如何搭建一个项目 使用工具:visual studio 2010 搭建过程:新建项目.配置设置(主要是解决运行后一闪而过的问题) 第二部分是 ...

  4. Linux redhat7.2 制作u盘问题总结

    Linux  redhat7.2  制作u盘问题总结 其实呢,觉得本来没必要写一篇关于装系统的文章,毕竟我觉得大多数搞it的人都会,比如win10.ubuntu做个启动盘啥的应该都会,但是说实在的今天 ...

  5. 数据分析02-(pandas介绍、jupyter notebook)

    数据分析-02 数据分析-02 pandas pandas介绍 pandas核心数据结构 Series DataFrame 核心数据结构操作 复合索引 Jupyter notebook 数据加载 处理 ...

  6. 【必知必会的MySQL知识】①初探MySQL

    目录 前言 MySQL是什么? MySQL版本 表的概念 表中的列和数据类型 行 主键 什么是SQL 实践操作 小结 前言 周所周知MySQL已成为全世界最受欢迎的数据库之一.无论你用的何种编程语言在 ...

  7. sqlilabs第一关

    首先打开网页,进行注入点的测试 输入?id=1 and 1=1发现1=2的时候没有进行报错,有两种可能,一种是不能注入,第二种是字符型可以通过对字符型里面的''进行闭合,输入'and 1=1--+发现 ...

  8. spring事务传播的Propagation.REQUIRES_NEW以及NEVER MANDATORY验证,及其失效的诡异问题

    NEVER 不使用事务,如果当前事务存在,则抛出异常 验证: @Service public class PrService { @Autowired PrDao dao; @Transactiona ...

  9. selenium 多窗口处理与网页frame

    多窗口处理 点击某些链接,会重新打开一个窗口,对于这种情况.想在薪页面操作,就得先切换窗口了. 获取窗口得唯一标识用句柄表示,所以只需要切换句柄,就可以在多个页面进行操作了 1. 先获取到当前得窗口句 ...

  10. 2022-11-20:小团生日收到妈妈送的两个一模一样的数列作为礼物! 他很开心的把玩,不过不小心没拿稳将数列摔坏了! 现在他手上的两个数列分别为A和B,长度分别为n和m。 小团很想再次让这两个数列变

    2022-11-20:小团生日收到妈妈送的两个一模一样的数列作为礼物! 他很开心的把玩,不过不小心没拿稳将数列摔坏了! 现在他手上的两个数列分别为A和B,长度分别为n和m. 小团很想再次让这两个数列变 ...