首页 > 其他分享 >【SpringBoot】条件装配 @profile

【SpringBoot】条件装配 @profile

时间:2023-02-09 10:56:58浏览次数:42  
标签:profile 装配 SpringBoot dev ProfileDto Profile 注解 public

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

标签:profile,装配,SpringBoot,dev,ProfileDto,Profile,注解,public
From: https://www.cnblogs.com/HelloWxl/p/17104480.html

相关文章

  • kafka-消息中间键(springboot集成)
    特性追求高吞吐量,适合产生大量数据的互联网服务的数据收集业务kafka入门1.导入依赖<dependencies><dependency><groupId>org.springframework.boot</gro......
  • SpringBoot整合JavaMail
    1、发送简单邮件导入依赖implementation'org.springframework.boot:spring-boot-starter-mail:3.0.2'开启相关协议,获取密码~我是用的是QQ邮箱,其他的也一样配置一下......
  • SpringBoot整合简单的定时任务~
    定时任务框架很多种Quartz,SpringTask,xxljob,PowerJob...1、JDK提供的timer//JDK提供的Timertimer=newTimer();//timer.schedule(newTimerTask......
  • springboot 动态获取配置信息完成启动
    架构说设计到数据量较大的应用要从k8s中迁出单独机器部署于是将8节点的服务准备迁出,且端口号在数据库中保存在不引入springcloud的方式下启动spring容器中对args进行配......
  • SimSolid如何快速处理大型复杂装配模型丨衡祖仿真
    SimSolid是一款基于有限元算法扩展算法的结构求解器,能进行快速的模型验证。与传统有限元软件不同,它采用无需简化的精确的几何模型开始建模,SimSolid软件不仅不需要进行网格......
  • SpringBoot工程入门case
    SpringBoot的设计目的是用来简化Spring应用的初始搭建以及开发过程。SpringBoot入门案例:1、创建一个新module  2、除pom和src文件剩余都删除。  3、在src.com......
  • springboot开发日记(7)
    springboot——自动配置在日记(2)中提到过,@SpringBootApplication由以下三个注解组合而成:@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan1.@Spr......
  • SpringBoot
    SpringBoot文章来源于:雷神:https://www.bilibili.com/video/BV19K4y1L7MT/?spm_id_from=333.337.search-card.all.click&vd_source=a9bff059910348f08db3690eefbeacbe特点......
  • 安装配置apache
    推荐步骤:​在Centos02安装DNS服务器,DNS区域的名字为bdqn.com,在DNS区域中添加www主机指向网站服务器​在Centos01安装apache服务器,优化apache服务,客户端使用www.bdqn.com访......
  • 安装配置Apache
    实验步骤给Centos01配置IP地址给Centos01修改IP地址和DNS第一章:安装配置Apache_DNS重新启动服务查看IP地址和DNS第一章:安装配置Apache_配置文件_02查看配置的IP地址和DNS第......