@Profile使用及SpringBoot获取profile值

之前开发用过 maven 的环境隔离,现在使用springboot的@Profile功能,发现spring体系真的大到我只是学习了皮毛。相比面试问的 IOC,bean的作用域等,突然觉得很可笑。

官方文档关于 Profile 的使用
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-profiles

  1. @Profile注解使用范围:
    @Configration 和 @Component 注解的类及其方法,其中包括继承了@Component的注解:@Service、@Controller、@Repository等…

  2. @Profile可接受一个或者多个参数,例如:

@Service
@Profile({"prod","default"})

Demo

参考: https://blog.csdn.net/zknxx/article/details/77906096

先看项目架构体系,只需要看展示的类,其他不用管

在这里插入图片描述

application.properties 配置文件
spring.profiles.active=prod,default
service
public interface ProfileService {
    String getMsg();
}
service.impl
@Service
@Profile("dev")
public class DevServiceImpl implements ProfileService {

    public DevServiceImpl() {
        System.out.println("我是开发环境。。。。。");
    }

    @Override
    public String getMsg() {
        StringBuilder sb = new StringBuilder();
        sb.append("我在开发环境,").append("我只能吃加班餐:大米饭。。。。");
        return sb.toString();
    }
}

@Service
@Profile({"prod","default"})
public class ProdServiceImpl implements ProfileService {

    public ProdServiceImpl() {
        System.out.println("我是生产环境。。。。。");
    }

    @Override
    public String getMsg() {
        StringBuilder sb = new StringBuilder();
        sb.append("我在生产环境,").append("我可以吃鸡鸭鱼牛羊肉。。。。");
        return sb.toString();
    }

}

controller
@Profile("dev")
@RestController
public class DevController {

    @Autowired
    private ProfileService profileService;

    @RequestMapping(value = "/")
    public String dev(){
        return "hello-dev\n"+profileService.getMsg();
    }

}

@Profile("prod")
@RestController
public class ProdController {

    @Autowired
    private ProfileService profileService;

    @RequestMapping(value = "/")
    public String prod(){
        return "hello-prod\n"+profileService.getMsg();
    }

}


在application.properties 配置文件使用不同的环境会执行不同的方法。

那么问题来了,这个功能使如何实现的?如果让你设计你会怎么做?面试新题,啊哈哈哈哈

获取profile值

参考来源:一下找不到了…囧

使用profile帮我解决了不同环境使用不同配置的问题,那么如果我们需要在代码中获取这个profile的值怎么处理呢?

@Component
public class ProfileUtil implements ApplicationContextAware {

    private static ApplicationContext context = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.context = applicationContext;
    }

    // 获取当前环境参数  exp: dev,prod,test
    public static String getActiveProfile() {
        String []profiles = context.getEnvironment().getActiveProfiles();
        if( ! ArrayUtils.isEmpty(profiles)){
            return profiles[0];
        }
        return "";
    }
}
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
SpringBoot实战(第4版)清晰文字版,第 1 章 入门 ................................................ 1 1.1 Spring 风云再起 ........................................ 1 1.1.1 重新认识 Spring ............................ 2 1.1.2 Spring Boot 精要 ........................... 3 1.1.3 Spring Boot 不是什么 ................... 6 1.2 Spring Boot 入门 ....................................... 6 1.2.1 安装 Spring Boot CLI .................... 7 1.2.2 使用 Spring Initializr 初始化 Spring Boot 项目 .......................... 10 1.3 小结 ......................................................... 18 第 2 章 开发第一个应用程序 .................... 19 2.1 运用 Spring Boot ..................................... 19 2.1.1 查看初始化的 Spring Boot 新项目 .......................................... 21 2.1.2 Spring Boot 项目构建过程 解析 .............................................. 24 2.2 使用起步依赖 .......................................... 27 2.2.1 指定基于功能的依赖 ................... 28 2.2.2 覆盖起步依赖引入的传递依赖 .... 29 2.3 使用自动配置 .......................................... 30 2.3.1 专注于应用程序功能 ................... 31 2.3.2 运行应用程序 .............................. 36 2.3.3 刚刚发生了什么 ........................... 38 2.4 小结 ......................................................... 41 第 3 章 自定义配置 .................................... 42 3.1 覆盖 Spring Boot 自动配置 ..................... 42 3.1.1 保护应用程序 .............................. 43 3.1.2 创建自定义的安全配置 ............... 44 3.1.3 掀开自动配置的神秘面纱 ........... 48 3.2 通过属性文件外置配置 ........................... 49 3.2.1 自动配置微调 .............................. 50 3.2.2 应用程序 Bean 的配置外置 ......... 55 3.2.3 使用 Profile 进行配置 .................. 59 3.3 定制应用程序错误页面 ........................... 62 3.4 小结 ......................................................... 64 第 4 章 测试 ............................................... 66 4.1 集成测试自动配置 .................................. 66 4.2 测试 Web 应用程序 ................................. 68 4.2.1 模拟 Spring MVC ........................ 69 4.2.2 测试 Web 安全 ............................. 72 4.3 测试运行中的应用程序 ........................... 74 4.3.1 用随机端口启动服务器 ............... 75 4.3.2 使用 Selenium 测试 HTML 页面 ............................................. 76 4.4 小结 ......................................................... 78 第 5 章 Groovy 与 Spring Boot CLI ......... 80 5.1 开发 Spring Boot CLI 应用程序 .............. 80 5.1.1 设置 CLI 项目 .............................. 81 5.1.2 通过 Groovy 消除代码噪声 ......... 81 5.1.3 发生了什么 .................................. 85 5.2 获取依赖 .................................................. 86 5.2.1 覆盖默认依赖版本 ....................... 87 5.2.2 添加依赖仓库 .............................. 88 5.3 用 CLI 运行测试 ...................................... 89 5.4 创建可部署的产物 .................................. 91 5.5 小结 ......................................................... 91 第 6 章 在 Spring Boot 中使用 Grails ...... 93 6.1 使用 GORM 进行数据持久化 ................. 93 2 目 录 6.2 使用 Groovy Server Pages 定义视图 ....... 98 6.3 结合 Spring Boot 与 Grails 3 ................. 100 6.3.1 创建新的 Grails 项目 ................. 100 6.3.2 定义领域模型 ............................ 103 6.3.3 开发 Grails 控制器 ..................... 104 6.3.4 创建视图 .................................... 105 6.4 小结 ....................................................... 107 第 7 章 深入 Actuator .............................. 108 7.1 揭秘 Actuator 的端点 ............................ 108 7.1.1 查看配置明细 ............................ 109 7.1.2 运行时度量 ................................ 115 7.1.3 关闭应用程序 ............................ 121 7.1.4 获取应用信息 ............................ 121 7.2 连接 Actuator 的远程 shell .................... 122 7.2.1 查看 autoconfig 报告 ........... 123 7.2.2 列出应用程序的 Bean ............... 124 7.2.3 查看应用程序的度量信息 ......... 124 7.2.4 调用 Actuator 端点 .................... 125 7.3 通过 JMX 监控应用程序 ....................... 126 7.4 定制 Actuator......................................... 128 7.4.1 修改端点 ID ............................... 128 7.4.2 启用和禁用端点 ........................ 129 7.4.3 添加自定义度量信息 ................. 129 7.4.4 创建自定义跟踪仓库 ................. 132 7.4.5 插入自定义健康指示器 ............. 134 7.5 保护 Actuator 端点 ................................ 136 7.6 小结 ....................................................... 138 第 8 章 部署 Spring Boot 应用程序 ........ 139 8.1 衡量多种部署方式 ................................ 139 8.2 部署到应用服务器 ................................ 140 8.2.1 构建 WAR 文件 ......................... 141 8.2.2 创建生产 Profile ........................ 142 8.2.3 开启数据库迁移 ........................ 145 8.3 推上云端 ............................................... 150 8.3.1 部署到 Cloud Foundry ............... 150 8.3.2 部署到 Heroku ........................... 153 8.4 小结 ....................................................... 155 附录 A Spring Boot 开发者工具.............. 157 附录 B Spring Boot 起步依赖 ................. 163 附录 C 配置属性 ...................................... 169 附录 D Spring Boot 依赖 ......................... 202
目录结构 尚硅谷_SpringBoot_入门-课程简介 尚硅谷_SpringBoot_入门-Spring Boot简介 尚硅谷_SpringBoot_入门-微服务简介 尚硅谷_SpringBoot_入门-环境准备 尚硅谷_SpringBoot_入门-springboot-helloworld 尚硅谷_SpringBoot_入门-HelloWorld细节-场景启动器(starter) 尚硅谷_SpringBoot_入门-HelloWorld细节-自动配置 尚硅谷_SpringBoot_入门-使用向导快速创建Spring Boot应用 尚硅谷_SpringBoot_配置-yaml简介 尚硅谷_SpringBoot_配置-yaml语法 尚硅谷_SpringBoot_配置-yaml配置文件获取 尚硅谷_SpringBoot_配置-properties配置文件编码问题 尚硅谷_SpringBoot_配置-@ConfigurationProperties与@Value区别 尚硅谷_SpringBoot_配置-@PropertySource、@ImportResource、@Bean 尚硅谷_SpringBoot_配置-配置文件占位符 尚硅谷_SpringBoot_配置-Profile多环境支持 尚硅谷_SpringBoot_配置-配置文件的加载位置 尚硅谷_SpringBoot_配置-外部配置加载顺序 尚硅谷_SpringBoot_配置-自动配置原理 尚硅谷_SpringBoot_配置-@Conditional&自动配置报告 尚硅谷_SpringBoot_日志-日志框架分类和选择 尚硅谷_SpringBoot_日志-slf4j使用原理 尚硅谷_SpringBoot_日志-其他日志框架统一转换为slf4j 尚硅谷_SpringBoot_日志-SpringBoot日志关系 尚硅谷_SpringBoot_日志-SpringBoot默认配置 尚硅谷_SpringBoot_日志-指定日志文件和日志Profile功能 尚硅谷_SpringBoot_日志-切换日志框架 尚硅谷_SpringBoot_web开发-简介 尚硅谷_SpringBoot_web开发-webjars&静态资源映射规则 尚硅谷_SpringBoot_web开发-引入thymeleaf 尚硅谷_SpringBoot_web开发-thymeleaf语法 尚硅谷_SpringBoot_web开发-SpringMVC自动配置原理 尚硅谷_SpringBoot_web开发-扩展与全面接管SpringMVC 尚硅谷_SpringBoot_web开发-【实验】-引入资源 尚硅谷_SpringBoot_web开发-【实验】-国际化 尚硅谷_SpringBoot_web开发-【实验】-登陆&拦截器 尚硅谷_SpringBoot_web开发-【实验】-Restful实验要求 尚硅谷_SpringBoot_web开发-【实验】-员工列表-公共页抽 尚硅谷_SpringBoot_web开发-【实验】-员工列表-链接高亮&列表完成 尚硅谷_SpringBoot_web开发-【实验】-员工添加-来到添加页面 尚硅谷_SpringBoot_web开发-【实验】-员工添加-添加完成 尚硅谷_SpringBoot_web开发-【实验】-员工修改-重用页面&修改完成 尚硅谷_SpringBoot_web开发-【实验】-员工删除-删除完成 尚硅谷_SpringBoot_web开发-错误处理原理&定制错误页面 尚硅谷_SpringBoot_web开发-定制错误数据 尚硅谷_SpringBoot_web开发-嵌入式Servlet容器配置修改 尚硅谷_SpringBoot_web开发-注册servlet三大组件 尚硅谷_SpringBoot_web开发-切换其他嵌入式Servlet容器 尚硅谷_SpringBoot_web开发-嵌入式Servlet容器自动配置原理 尚硅谷_SpringBoot_web开发-嵌入式Servlet容器启动原理 尚硅谷_SpringBoot_web开发-使用外部Servlet容器&JSP;支持 尚硅谷_SpringBoot_web开发-外部Servlet容器启动SpringBoot应用原理 尚硅谷_SpringBoot_Docker-简介 尚硅谷_SpringBoot_Docker-核心概念 尚硅谷_SpringBoot_Docker-linux环境准备 尚硅谷_SpringBoot_Docker-docker安装&启动&停止 尚硅谷_SpringBoot_Docker-docker镜像操作常用命令 尚硅谷_SpringBoot_Docker-docker容器操作常用命令 尚硅谷_SpringBoot_Docker-docker安装MySQL 尚硅谷_SpringBoot_数据访问-简介 尚硅谷_SpringBoot_数据访问-JDBC&自动配置原理 尚硅谷_SpringBoot_数据访问-整合Druid&配置数据源监控 尚硅谷_SpringBoot_数据访问-整合MyBatis(一)-基础环境搭建 尚硅谷_SpringBoot_数据访问-整合MyBatis(二)-注解版MyBatis 尚硅谷_SpringBoot_数据访问-整合MyBatis(二)-配置版MyBatis 尚硅谷_SpringBoot_数据访问-SpringData JPA简介 尚硅谷_SpringBoot_数据访问-整合JPA 尚硅谷_SpringBoot_原理-第一步:创建SpringApplication 尚硅谷_SpringBoot_原理-第二步:启动应用 尚硅谷_SpringBoot_原理-事件监听机制相关测试 尚硅谷_SpringBoot_原理-自定义starter 尚硅谷_SpringBoot_结束语
在Spring Boot中,可以使用@Value注解来获取环境变量。在旧版本中,可以使用@Value("${spring.profiles.active}")来获取spring.profiles.active的。然而,在升级到新版本后,这种方式可能会提示异常。在新版本中,可以使用新的配置方式来获取环境变量。具体来说,可以使用@Value("${spring.config.activate.on-profile}")来获取spring.profiles.active的。\[1\] 另外,还可以通过在java程序启动时指定参数来获取classpath下的文件。这可以通过在启动命令中使用-D参数来指定系统属性,然后在代码中使用@Value注解来获取该属性的。\[2\] 此外,还可以使用@Configuration注解和@Profile注解来实现特异化配置。通过在配置类上使用@Profile注解,并指定特定的profile名称,可以使得该配置类只在对应的profile激活时生效。\[3\] #### 引用[.reference_title] - *1* *2* [spring boot2.5x、2.6x获取环境变量(spring.profiles.active),获取classpath下文件方法](https://blog.csdn.net/qq_41070393/article/details/123950153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【springbootSpringBoot 配置文件中 spring.profiles.active 配置详解](https://blog.csdn.net/qyj19920704/article/details/127699920)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值