介绍

@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 { @Autowired
ProFileEntity proFileEntity; @RequestMapping("/test")
public String test(){
return proFileEntity.getName();
} }

六.成功

@Profile-根据不同环境注入bean的更多相关文章

  1. 25、自动装配-@Profile根据环境注册bean

    25.自动装配-@Profile根据环境注册bean 指定组件在哪个环境的情况下才能被注册到容器中 加了环境标识的,只有这个环境被激活才能注册到组件中 默认是default环境 写在类上,整个配置类的 ...

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

    接上回继续,项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre) ...

  3. Maven 整合 spring profile实现多环境自动切换

    Maven 整合 spring profile实现多环境自动切换 时间:2014-03-19 15:32来源:Internet 作者:Internet 点击:525次 profile主要用在项目多环境 ...

  4. 利用maven的resources、filter和profile实现不同环境使用不同配置文件

    基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...

  5. maven profile实现多环境打包

    快速解决: 项目目录 1.pom文件中添加profile <profiles> <profile> <!-- 本地开发环境 --> <id>dev< ...

  6. Spring Boot入门(二):使用Profile实现多环境配置管理&如何获取配置文件值

    在上一篇博客Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件中,我们新建了一个最原始的Spring Boot项目,并使用了更为流行的yaml配置文件. ...

  7. Maven之profile实现多环境配置动态切换

            一般的软件项目,在开发.测试及生产等环境下配置文件中参数是不同的.传统的做法是在项目部署的时候,手动修改或者替换这个配置文件.这样太麻烦了,我们可以用Maven的profile来解决这 ...

  8. maven学习利用Profile构建不同环境的部署包

    项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre).正式生产环 ...

  9. Spring中如何动态注入Bean实例教程

    前言 在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式,容易出现未被正确注入成功的情况. 本文将介绍一种在实际项目中基于动态的方式来提取Spring管理的Be ...

随机推荐

  1. python使用笔记002

    一.字符串常用方法 1 s = ' ha.hahaha.' 2 print(s.count('a'))#找某一个元素在字符串里出现的次数 3 print(s.index('a'))#找某一个元素的下标 ...

  2. EXCEL:关键字有重复,其他信息一行多列显示

    =INDEX(A:A,SMALL(IF(MATCH($A$2:$A$13,$A$2:$A$13,0)=ROW($A$2:$A$13)-1,ROW($2:$13),4^8),ROW(1:1)))& ...

  3. C语言:2.2

    #include <stdio.h> int main() { unsigned short bla=32768; short blb=32768; printf("%d %d\ ...

  4. C语言 c++区别

    C语言是C89标准,C++是C++99标准的.C89就是在1989年制定的标准,如今最新的是C11和C++11标准.根据不同的标准,它们的功能也会有所不同,但是越新的版本支持的编译器越少

  5. matlab——线性规划

    @ 目录 前言 一.基本概念 二.matlab实现 1.常用函数 2.常见变形 参考书目 前言 线性规划是数学规划中的一个重要分支,常用于解决如何利用现有资源来安排生产,以取得最大经济效益的问题.本文 ...

  6. Spark—RDD编程常用转换算子代码实例

    Spark-RDD编程常用转换算子代码实例 Spark rdd 常用 Transformation 实例: 1.def map[U: ClassTag](f: T => U): RDD[U]  ...

  7. 【NOIP2007】Hanoi双塔问题

    题目描述 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的圆盘,注意这两个圆盘是不加区分的(下图为n=3的情形). 现要将这些圆盘移到C柱上 ...

  8. odoo里面context用法

    原文转自:https://www.cnblogs.com/zhaoweihang/p/9698852.html <field name="partner_id" string ...

  9. (python函数04)zip(*sorted(zip()))

    zip(*sorted(zip())) 用这个玩意儿可以以对两个迭代对象进行排序. 示例代码01  cnts = [2, 4, 3, 6, 5] boundingBoxes = [(730, 20, ...

  10. maven 标签 关于<import>标签

      标签用途:在dependecyManagement元素下用,合并此import标签上级dependency的groupId和artid中指向依赖的dependecyManagement内容   标 ...