【问题标题】:Spring profiles: Which <beans> element should be parsed?Spring 配置文件:应该解析哪个 <beans> 元素?
【发布时间】:2016-03-22 08:23:52
【问题描述】:

我的 spring 上下文中有以下配置:

<beans profile="!prof1" >
     <security:authentication-manager id="authenticationManager" erase-credentials="true">
                <security:authentication-provider ref="1" />
                <security:authentication-provider ref="2" />
                <security:authentication-provider ref="3" />
            </security:authentication-manager>
</beans>

<beans profile="prof1" >
     <security:authentication-manager id="authenticationManager" erase-credentials="true">
                <security:authentication-provider ref="0" />
                <security:authentication-provider ref="1" />
                <security:authentication-provider ref="2" />
                <security:authentication-provider ref="3" />
            </security:authentication-manager>
</beans>

存在将解析哪些&lt;beans&gt; 元素的问题: prof1prof2 配置文件已激活。

看起来它总是选择这个&lt;beans profile="prof1" &gt;,但不知道为什么它不选择另一个&lt;beans profile="!prof1" &gt;。我可以转发它总是会选择&lt;beans&gt; 而不带感叹号吗?

【问题讨论】:

    标签: java spring spring-profiles


    【解决方案1】:

    Javadoc for Profile 声明

    如果给定配置文件以 NOT 运算符 (!) 为前缀,则在配置文件未激活时将注册带注释的组件

    spring-beans XSD 声明与 XML bean 定义相同,但更难阅读。)

    据我了解,只有 prof1 被视为带有 profile="!prof1" 的 beans 元素。您还激活 prof2 与此 bean 定义无关。

    【讨论】:

    • 感谢迈克尔的澄清!
    【解决方案2】:

    如果您的有效个人资料是 prof1 和 prof2,则以下内容将有效

    <beans profile="prof1" >
     <security:authentication-manager id="authenticationManager" erase-credentials="true">
                <security:authentication-provider ref="0" />
                <security:authentication-provider ref="1" />
                <security:authentication-provider ref="2" />
                <security:authentication-provider ref="3" />
            </security:authentication-manager>
    

    【讨论】:

    • 谢谢埃塞克斯!您能否分享一些文档参考资料,即为什么会选择此个人资料?因为目前,我看到 spring 选择了这个元素 ,但不确定它是否总是正确的。
    • 我没有看文档,我跑了一个测试。我没有意识到它是这样工作的!它符合您的预期,即 !prof1 将在测试配置文件中处于活动状态。
    猜你喜欢
    • 2013-02-12
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    相关资源
    最近更新 更多