【问题标题】:Spring 3 applicationContext-security-JDBC.xml has beans:bean not bean?Spring 3 applicationContext-security-JDBC.xml 有 bean:bean 不是 bean?
【发布时间】:2011-08-02 05:01:21
【问题描述】:

谁能告诉我在我的 ApplicationContext 中我必须使用 beans:bean 而不是 bean 以及如何修复它。

<?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"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/jdbc
           http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/friends/**" access="hasRole('ROLE_USER')" />

        <form-login login-page="/login.html"
        default-target-url="/index.html" always-use-default-target="true"
            authentication-failure-url="/login.html?authfailed=true" />

    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource" />
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <beans:property name="location" value="classpath:jdbc.properties" />
    </beans:bean>


    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <beans:property name="driverClassName" value="${database.driver}" />
        <beans:property name="url" value="${database.url}" />
        <beans:property name="username" value="${database.user}" />
        <beans:property name="password" value="${database.password}" />
        <beans:property name="initialSize" value="5" />
        <beans:property name="maxActive" value="10" />
    </beans:bean>




</beans:beans>

【问题讨论】:

    标签: java xml spring configuration xml-namespaces


    【解决方案1】:

    解释。 基本上,您在这里处理的是 XML 命名空间。 Spring 配置允许您使用来自不同命名空间的配置元素作为扩展基本 beans 命名空间配置的一种方式,该配置具有方便的特定于域的配置,例如上述案例中的安全配置。

    如果您的配置文件集中在这些扩展命名空间之一上 - 再次,让我们以安全性为例 - 如果您将默认命名空间声明为扩展命名空间而不是标准 @,它可以清理文件987654322@ 命名空间。就是这样

    xmlns="http://www.springframework.org/schema/security"
    

    确实——它使安全性成为默认命名空间,这意味着您不必为其添加前缀sec:security:

    但是当您将security 设为默认值时,您必须在使用beans 命名空间元素时明确。因此beans: 前缀。

    解决方案。如果您希望 beans 成为默认名称,只需将默认命名空间更改为 beans

    xmlns="http://www.springframework.org/schema/beans"
    

    替代解决方案。或者,如果您想输入更短的内容,您可以这样做

    xmlns:b="http://www.springframework.org/schema/beans"
    

    而不是

    xmlns:beans="http://www.springframework.org/schema/beans"
    

    这将允许您做类似的事情

    <b:bean id="beanId" class="x.y.z.BeanClass" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2015-04-26
      • 2017-04-17
      • 1970-01-01
      相关资源
      最近更新 更多