有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpring概要文件:应该解析哪个<beans>元素?

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

在分析<beans>元素时会考虑以下问题: prof1prof2配置文件被激活

看起来它总是选择这个<beans profile="prof1" >,但不确定为什么不选择另一个<beans profile="!prof1" >。我可以转告它总是选择<beans>而不带感叹号吗


共 (2) 个答案

  1. # 1 楼答案

    Javadoc for Profile声明:

    If a given profile is prefixed with the NOT operator (!), the annotated component will be registered if the profile is not active

    (对于XMLBean定义,spring-beans XSD声明了相同的内容,但更难阅读。)

    据我所知,对于带有profile=“!prof1”的bean元素,只查看prof1。同时激活prof2与此bean定义无关

  2. # 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>