Spring配置文件:应该解析哪个<beans>元素?

时间:2016-03-22 08:23:52

标签: java spring spring-profiles

我在春天的上下文中有以下配置:

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

有问题考虑哪个<beans>元素将被解析: prof1 prof2 个人资料已激活。

看起来总是选择这一个<beans profile="prof1" >,但不确定为什么它不会选择另一个<beans profile="!prof1" >。我可以传达它总是会选择<beans>没有感叹号吗?

2 个答案:

答案 0 :(得分:2)

Javadoc for Profile表示

  

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

spring-beans XSD表示XML bean定义相同,但更难阅读。)

据我了解,只有 profile =&#34;!prof1&#34; 的bean元素才会查看 prof1 。您还激活 prof2 与此bean定义无关。

答案 1 :(得分:1)

如果您的有效个人资料是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>