• Springboot @Profile使用详解


    简介

    @Profile这个注解主要是和spring.profile.active这个配置结合使用的,让我们的配置文件使用的更加灵活,比如原来我们写:spring.profile.active=dev,那么整个项目都会使用application-dev.properites下的配置文件,但是如果我们在类或者方法上加上@Profile("dev, prod, xxxx")注解,这样就比较灵活了,比如在一个bean上加上@Profile("prod"),那么这个bean只会在 spring.profile.active=prod的时候才被实例化,以此类推

    在项目中,有时我们需要能根据当前环境,动态的激活和切换一系列组件,这个借助 Spring 提供的 @Profile 注解即可实现,下面通过样例进行演示。

    @Profile 注解的作用是指定组件在哪个环境的情况下才能被注册到容器中,若不指定,任何环境下都能注册这个组件。

    • 加了@Profile 注解的 bean,只有这个环境被激活的时候才能注册到容器中。默认是 default 环境。
    • 若 @Profile 注解写在配置类上,只有在指定的环境的时候,整个配置类里面的所有配置才能开始生效。

    演示

    根据当前环境的不同(dev 或 prod),自动实例化对应的 DataSource

    1. @Configuration
    2. @PropertySource(value = {"classpath:/dbconfig.properties"})
    3. public class ProfileBeanConfig implements EmbeddedValueResolverAware {
    4. //数据库连接用户名
    5. @Value(value = "${jdbc.username}")
    6. private String username;
    7. //数据库连接密码
    8. private String password;
    9. //开发环境数据源
    10. @Bean(value = "dataSourceDev")
    11. @Profile(value = "dev")
    12. public DataSource dataSourceDev(@Value("${jdbc.driverClass}") String driverClass)
    13. throws PropertyVetoException {
    14. ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    15. comboPooledDataSource.setUser(this.username);
    16. comboPooledDataSource.setPassword(this.password);
    17. comboPooledDataSource.setDriverClass(driverClass);
    18. comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev");
    19. return comboPooledDataSource;
    20. }
    21. //生产环境数据源
    22. @Bean(value = "dataSourceProduction")
    23. @Profile("prod")
    24. public DataSource dataSourceProduction(@Value("${jdbc.driverClass}") String driverClass)
    25. throws PropertyVetoException {
    26. ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    27. comboPooledDataSource.setUser(this.username);
    28. comboPooledDataSource.setPassword(this.password);
    29. comboPooledDataSource.setDriverClass(driverClass);
    30. comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/production");
    31. return comboPooledDataSource;
    32. }
    33. }

    有时候我们会有这样的一个需求,在生产环境或者开发环境我们所看到的功能是不同的,这便需要我们根据配置项来激活不同的功能。


    比如一些接口在开发环境中无法调用,那么在开发环境中就需要 mock 数据。因此我们可以写一个 mock 数据接口类,专门供开发环境使用。而生产环境则使用真实的接口调用类,二者通过 @Profile 注解来自动激活生效。

    首先我们先定义一个 Service 的接口:

    1. public interface ProductService {
    2. String getProductInfo(Long id);
    3. }

    接着我们创建两个实现类,分别对应开发环境和生产环境:

    1. @Service
    2. @Profile("dev")
    3. public class MockProductServiceImpl implements ProductService {
    4. @Override
    5. public String getProductInfo(Long id) {
    6. return "这是开发环境数据:" + id;
    7. }
    8. }
    9. @Service
    10. @Profile("prod")
    11. public class ProductServiceImpl implements ProductService {
    12. @Override
    13. public String getProductInfo(Long id) {
    14. //return productResource.getProductInfo(id);
    15. return "这是生产环境数据:" + id;
    16. }
    17. }

    最后创建一个 Controller 调用 Service 接口:

    1. @RestController
    2. public class TestController {
    3. @Autowired
    4. private ProductService productService;
    5. @GetMapping("/hello")
    6. public String hello(@RequestParam("id") Long id) {
    7. return productService.getProductInfo(id);
    8. }
    9. }

    开始测试,首先我们编辑 application.properties 文件

    spring.profiles.active=dev

    访问 Controller 接口可以看到调用的是 MockProductServiceImpl 这个 Servcie: 

    原文:SpringBoot - @Profile注解使用详解(根据环境动态激活使用相应组件)

    spring.profiles.active=prod

    访问 Controller 接口可以看到调用的是 ProductServiceImpl 这个 Servcie:

    原文:SpringBoot - @Profile注解使用详解(根据环境动态激活使用相应组件)

  • 相关阅读:
    苹果系统_安装matplotlib_&_pygame,以pycharm导入模块
    Xinetd服务介绍
    CF33b-B. String Problem
    奶爸级教学---webpack详细教学
    AJAX 入门笔记
    web前端设计与开发期末作品_期末大作业-疫情
    Activiti 7 源码学习
    学C++要不要先学C语言?
    力扣题目学习笔记(OC + Swift)
    GLSL (2)数据类型
  • 原文地址:https://blog.csdn.net/Maxiao1204/article/details/125902101