栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将@Profile批注与属性占位符值一起使用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将@Profile批注与属性占位符值一起使用

通过Spring的源代码,我得出的结论是您所要求的是不可能的。为了清楚起见,不可能让Spring

${property}
内部进行评估
@Profile

具体看看

ProfileCondition
哪个检查配置文件是否处于活动状态。

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;    }}

肉是

context.getEnvironment().acceptsProfiles(((String[]) value))

现在,如果你检查的来源

AbstractEnvironment
在那里
acceptsProfiles
居住,你会发现,在控制到达

protected boolean isProfileActive(String profile) {    validateProfile(profile);    return doGetActiveProfiles().contains(profile) || (doGetActiveProfiles().isEmpty() && doGetDefaultProfiles().contains(profile));}

它不尝试计算表达式,而是采用逐字字符串形式(还请注意,

isProfileActive
对字符串表达式的评估也无处不在)

您可以在这里和这里找到我上面提到的代码。


另一个说明,我不确定为什么需要一个动态的配置文件名称。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/635589.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号