背景
那么profile的组合如何使用呢???
比如我们这样使用
?
1
|
@profile ({ "prod" , "unit-test" })
|
分析
上述的profile大家应该不会存有疑问 当profile为prod或者unit-test的时候才会生效。
但是如果我们使用非呢~如何确保在某些情况下不生效!
spring提供了常见的!来进行描述
因此如果想要在非生产环境生效只要简单的写成
?
1
|
@profile ({ "!prod" })
|
那么如何在多个环境下不生效呢???
自作聪明的某些人【我】如下代码
?
1
|
@profile ({ "!prod" , "!unit-test" })
|
那么实际情况是否如此呢???
我们看一下对应的代码
代码
profile是通过profilecondition来完成控制的
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class profilecondition implements condition {
@override
public boolean matches(conditioncontext context, annotatedtypemetadata metadata) {
if (context.getenvironment() != null ) {
multivaluemap<string, object> 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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/**
* 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 <em>not</em> active.
* for example, <pre class="code">env.acceptsprofiles("p1", "!p2")</pre> 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的条件
当然代码如下
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@override
public 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
因此上述逻辑告诉我们其实应该是或者的逻辑。因此
?
1
|
@profile ({ "!prod" , "!unit-test" })
|
!prod||!unit-test===>!(prod&&unit-test) 也就是说当prod和unit-test都生效的时候才不会注册 其他调均都会注册生效
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://my.oschina.net/qixiaobo025/blog/1927580
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
Win10电脑关机太慢怎么办?Win10电脑快速关机方法教程
2025-05-27 49 -
2025-05-27 19
-
2025-05-27 25
-
2025-05-25 15
-
2025-05-29 48
热门评论