介绍

@Profile元注解是在不同的生产环境中,@Bean创建的SpringBean根据spring.profiles.active指定的环境不同创建不同环境的bean对象

一.@Profile元注解需要配合spring.profiles.active一起使用

二.首先在resources下创建三个配置文件

application.properties:

spring.profiles.active=prod

其他两个配置文件创建了就行!

三.创建需要注入的实体类

package com.wzq.dome.entity;import lombok.Data;/*** @description:* @author: Wzq* @create: 2019-12-12 15:00*/
@Data
public class ProFileEntity {private String name;}

四.根据spring.profiles.active指定不同的环境选择注入的bean

DomeApplication:代码如下

package com.wzq;import com.wzq.dome.entity.ProFileEntity;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;@SpringBootApplication
public class DomeApplication {public static void main(String[] args) {SpringApplication.run(DomeApplication.class, args);}@Bean(name = "proFileEntity")@Profile("prod")public ProFileEntity proFileEntity1(){ProFileEntity proFileEntity = new ProFileEntity();proFileEntity.setName("GoslingWu");return proFileEntity;}@Bean(name = "proFileEntity")@Profile("dev")public ProFileEntity proFileEntity(){ProFileEntity proFileEntity = new ProFileEntity();proFileEntity.setName("wzq");return proFileEntity;}}

五.使用

package com.wzq.dome.action;import com.wzq.dome.entity.ProFileEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @description:* @author: Wzq* @create: 2019-12-12 10:27*/
@RestController
public class TestController {@AutowiredProFileEntity proFileEntity;@RequestMapping("/test")public String test(){return proFileEntity.getName();}}

六.成功

@Profile-根据不同环境注入bean相关推荐

  1. @Profile 根据不同环境注入bean

    介绍 @Profile元注解是在不同的生产环境中,@Bean创建的SpringBean根据spring.profiles.active指定的环境不同创建不同环境的bean对象 一.@Profile元注 ...

  2. maven学习(下)利用Profile构建不同环境的部署包

    2019独角兽企业重金招聘Python工程师标准>>> 接上回继续,项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境 ...

  3. 使用maven的profile区分本地环境和线上环境

    使用maven的profile区分本地环境和线上环境 多环境开发,使用maven-profile,就可以在打包的时候通过参数的调整,最终打的包也不同. 以区分本地数据库和线上数据库为例 比如测试环境, ...

  4. java手工注入bean_java相关:Spring中如何动态注入Bean实例教程

    java相关:Spring中如何动态注入Bean实例教程 发布于 2020-3-8| 复制链接 摘记: 前言在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式, ...

  5. 在kotlin companion object中读取Bean,注入Bean对象

    在kotlin companion object中读取Bean,注入Bean对象 在使用kotlin时,或多或少地会使用到一些公共组件,如 http. mongo. redis相关的组件.   使用组 ...

  6. Springboot使用Maven Profile和Spring Profile进行多环境配置

    Springboot使用Maven Profile和Spring Profile进行多环境配置 目的 在实际的项目上,一般会分三种环境dev.test.prod来方便我们的开发和部署,要求我们在开发的 ...

  7. spring-boot 引入xml注入bean

    2019独角兽企业重金招聘Python工程师标准>>> spring-boot 引入xml注入bean 配置 public class TestServiceImpl impleme ...

  8. java filter注入,Spring-boot中Filter注入bean

    Spring-boot中Filter注入bean 在spring中使用Filter的方式不用再多说,但是通常情况下我们在使用filter中都可能会注入部分配置的类或者部分具有特殊功能的业务类. 在这种 ...

  9. 解决SpringBoot使用Quartz无法注入Bean的问题

    2019独角兽企业重金招聘Python工程师标准>>> 依赖 <dependency> <groupId>org.quartz-scheduler</g ...

最新文章

  1. u-charts 曲线图中间有部分没数据,导致点和点无法连成线的问题解决
  2. JGG | 这么漂亮的Venn网络竟然可以一步在线绘制?
  3. 计算机课怎样回归老师控制,职业学校计算机专业课堂回归真学习
  4. Check for Palindromes(算法)
  5. 从零点五开始用Unity做半个2D战棋小游戏(七)
  6. java与c语言工作量对比比例,对比平台-- C ++与Java之间的差异
  7. 单调栈 leetcode整理(三)
  8. linux libstdc 怎样升级,linux 更新libstdc++.so.6
  9. 《面向对象程序设计》第六次作业(图形化界面)
  10. java读取redis数据_Java连接redis操作数据
  11. DELMIA软件物流仿真:操作对象显示与隐藏功能介绍与使用方法
  12. vscode如何使用ajax,Ajax 的初步实现(使用vscode+node.js+express框架)
  13. 2013-2014中文核心期刊目录。。。。投稿时注意了啊
  14. 空军军医大学徐肖攀老师课题组招收医学影像方向研究生/研究助理
  15. pandas读取数据时,报错UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: invalid contin
  16. 网关系统就该这么设计(万能通用),贼稳!
  17. 我做了一款iOS12捷径市场,也许是目前最好看也是最具技术含量的ShortCuts小程序
  18. kali 配置静态ip的坑
  19. 计算机ei浏览器没有了,ie浏览器不见了_我的电脑桌面上怎么IE浏览器没有了啊...
  20. 创造属于自己的注册码

热门文章

  1. MetaWRAP分箱流程实战和结果解读
  2. PNAS | 菌群大战:“单打独斗之殇”与“分而治之之利”
  3. python使用matplotlib可视化3D柱状图(3D histogram、三维柱状图、包含三个坐标轴x、y、z)、设置zdir参数为z、改变3d图观察的角度
  4. R语言ggplot2可视化散点图、移除可视化图像中的网格线(remove gridlines)
  5. seaborn可视化displot绘制直方图(histogram)并通过axvline函数在直方图中添加均值(mean)竖线(自定义均值竖线色彩)
  6. R语言数据描述性统计(Descriptive statistics)实战:数据全局描述信息、数值数据的描述性统计(Numerical data)、离散型数据的描述性统计(Categorical)
  7. R语言dplyr包对数据进行超前或者之后处理(lead、lag)实战
  8. NLP诗词生成模型数据准备及实战
  9. 影像组学视频学习笔记(6)-特征筛选之LASSO回归(理论)、Li‘s have a solution and plan.
  10. R语言创建频数表和列联表