spring profile的示例分析
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章给大家分享的是有关spring profile的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。背景spring的profile大家都是用的溜的飞起~那么pr
千家信息网最后更新 2026年11月01日spring profile的示例分析
这篇文章给大家分享的是有关spring profile的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
背景
spring的profile大家都是用的溜的飞起~
那么profile的组合如何使用呢???
比如我们这样使用
@Profile({"prod", "unit-test"})分析
上述的profile大家应该不会存有疑问 当profile为prod或者unit-test的时候才会生效。
但是如果我们使用非呢~如何确保在某些情况下不生效!
spring提供了常见的!来进行描述
因此如果想要在非生产环境生效只要简单的写成
@Profile({"!prod"})那么如何在多个环境下不生效呢???
自作聪明的某些人【我】如下代码
@Profile({"!prod", "!unit-test"})那么实际情况是否如此呢???
我们看一下对应的代码
代码
profile是通过profileCondition来完成控制的
class ProfileCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { if (context.getEnvironment() != null) { MultiValueMap attrs = metadata.getAllAnnotationAttributes(Profile.class.getName()); if (attrs != null) { for (Object value : attrs.get("value")) { if (context.getEnvironment().acceptsProfiles(((String[]) value))) { return true; } } return false; } } return true; } } 很明显可以看到了acceptsProfiles
/** * Return whether one or more of the given profiles is active or, in the case of no * explicit active profiles, whether one or more of the given profiles is included in * the set of default profiles. If a profile begins with '!' the logic is inverted, * i.e. the method will return true if the given profile is not active. * For example, env.acceptsProfiles("p1", "!p2") will * return {@code true} if profile 'p1' is active or 'p2' is not active. * @throws IllegalArgumentException if called with zero arguments * or if any profile is {@code null}, empty or whitespace-only * @see #getActiveProfiles * @see #getDefaultProfiles */boolean acceptsProfiles(String... profiles);从上述可以看到应该是or的条件
当然代码如下
@Overridepublic boolean acceptsProfiles(String... profiles) { Assert.notEmpty(profiles, "Must specify at least one profile"); for (String profile : profiles) { if (StringUtils.hasLength(profile) && profile.charAt(0) == '!') { if (!isProfileActive(profile.substring(1))) { return true; } } else if (isProfileActive(profile)) { return true; } } return false;}因此可以看到当是!条件的时候会判断如果当前未激活profile返回true 否则当前是正常条件的换当前profile如果激活则返回true 当上述条件都不满足才返回false
因此上述逻辑告诉我们其实应该是或者的逻辑。因此
@Profile({"!prod", "!unit-test"})!prod||!unit-test===>!(prod&&unit-test) 也就是说当prod和unit-test都生效的时候才不会注册 其他调均都会注册生效
感谢各位的阅读!关于"spring profile的示例分析"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
代码
条件
分析
时候
示例
内容
情况
更多
激活
环境
篇文章
逻辑
不错
实用
明显
自作聪明
也就是
也就是说
多个
实际
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
湖北九亚互联网络科技有限公司
上海睿民互联网科技有限公司
亿人帮互联网科技有限公司
广州大一互联网络科技有限公司
中国 科技 互联网
互联网 卓越科技
艾特科技互联网
互联网金融科技创新
腾讯科技 互联网女皇
万达互联网科技
互联网科技美容
栖莱科技互联网股份有限公司
河南猎农互联网科技有限公司
湖南一路畅行互联网科技
速维有品互联网信息科技(苏州)有限公司
恒生互联网科技业指数调整周期
黑云互联网科技有限公司
卡尔迅互联网科技有限公司
伍佰万互联网科技是干嘛的
互联网 科技节活动 微视频大赛
互联网科技公司发展
奇酷互联网络科技
卓越科技(互联网 )
深圳国际航运科技和互联网
宁波青枫互联网科技有限公司
受益于互联网科技的高速发展
互联网科技革命什么开始
互联网科技职业技能培训学校
广州三三互联网科技有限公司
绵阳真晨互联网科技有限公司