介绍

@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. kong配置upstream实现简单的负载均衡

    目录 通过konga实现 1. 配置upstream 2. 配置Service发布 3. 配置Route,匹配规则 4. 验证结果 通过 Kong Admin API实现 1. 配置upstream ...

  2. java网络编程基础——基本网络支持

    基本网络支持 java.net包主要为网络编程提供支持. 1.InetAddress InetAddress类代表IP地址,还有两个子类:Inet4Address.Inet6Address. pack ...

  3. springMVC-1-servlet回顾

    SpringMVC重点学习 项目目标:SpringMVC+Vue+SpringBoot+SpringCloud+Linux spring:IOC+AOP SpringMVC:SpringMVC的执行流 ...

  4. 【超值分享】为何写服务器程序需要自己管理内存,从改造std::string字符串操作说起。。。

    服务器程序为何要进行内存管理,管中窥豹,让我们从string字符串的操作说起...... new/delete是用于c++中的动态内存管理函数,而malloc/free在c++和c中都可以使用,本质上 ...

  5. Python自动化测试面试题-用例设计篇

    目录 Python自动化测试面试题-经验篇 Python自动化测试面试题-用例设计篇 Python自动化测试面试题-Linux篇 Python自动化测试面试题-MySQL篇 Python自动化测试面试 ...

  6. linux下快速安装pyenv管理多版本python

    起因 一直服务器python项目都是放docker跑,这次为了测试,不得不在宿主机跑,就必须安装python3.7,但是ubuntu16.04下有点麻烦 尝试 源码安装,懒,算了,也不想污染服务器环境 ...

  7. python之 数据类型限制

    问题增加类型限制 NameError: name 'List' is not defined def twoSum(self, nums: List[int], target: int) -> ...

  8. Linux下获取当前程序的绝对路径

    在Linux开发应用时,我们常常需要在程序中获取当前程序绝对路径,我们可以通过readlink读取符号链接/proc/self/exe进行获取,这个符号链接代表当前程序,它的源路径就是当前程序的绝对路 ...

  9. 防止因提供的sql脚本有问题导致版本bvt失败技巧

    发版本时,可能会由于测试库和开发库表结构不一样而导致数据库脚本在测试那边执行时出错,导致版本BVT失败,以下技巧可解决此问题. 步骤:备份目标库,在备份库中执行将要提供的sql脚本看有无问题,若没问题 ...

  10. SpringBoot @ModelAttribute 用法

    前言 项目中遇到这么一个使用场景,用户的登录信息给予token保存,在需要有登录信息的地方,每次都要去获取用户Id,但每次在请求方法中去获取用户信息,代码重复,冗余,很low于是想到了用@ModelA ...