<beans> to <beans:beans> - 必须声明元素bean </beans:beans> </beans>

时间:2014-07-28 23:09:04

标签: spring

在下面的代码中,如果我将起始/结束标记从“beans”更改为“beans:beans”,为什么我必须“声明元素”(来自IntelliJ)?

“豆子”有什么意义?

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<!-- HTTP security configurations -->
<http auto-config="true" use-expressions="true">
    <form-login login-processing-url="/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t" />
    <logout logout-url="/j_spring_security_logout" />
    <intercept-url pattern="/login" requires-channel="https"/>
    <intercept-url pattern="/backend/**" access="isAuthenticated()" />
    <intercept-url pattern="/todoes/**" access="isAuthenticated()" />
    <intercept-url pattern="/resources/**" access="permitAll" />
    <intercept-url pattern="/**" access="permitAll" />
    <remember-me key="mySecondSecretWordThatShouldBeHidden" user-service-ref="userAccountDetailsService" />
</http>

<global-method-security secured-annotations="enabled" pre-post-annotations="enabled" />

<!-- Configure Authentication mechanism -->

<beans:bean name="passwordEncoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder">
    <beans:constructor-arg name="secret" value="myVerySecretWordThatShouldBeSomewhereHidden"/>
</beans:bean>

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="userAccountDetailsService">
        <password-encoder ref="passwordEncoder"/>
    </authentication-provider>
</authentication-manager>

1 个答案:

答案 0 :(得分:2)

您已使用xmlns=...将安全架构绑定到默认命名空间。这意味着您可以直接使用该命名空间中的元素而无需限定,例如<authentication-manager>

要使用在另一个模式中定义的元素,您需要将该模式绑定到另一个模式,并将其用作前缀。声明xmlns:beans="http://www.springframework.org/schema/beans"将URL标识的架构绑定到命名空间beans。架构的位置在xsi:schemaLocation中。用法示例<beans:bean>。如果您将名称空间声明为xmlns:wibble="http://www.springframework.org/schema/beans",那么这将更改为<wibble:bean>

您可以使用它们中的任何一个作为默认命名空间,其中一个有意义取决于您的配置文件及其将具有的bean类型。