Spring 3 applicationContext-security-JDBC.xml有bean:bean不是bean? [英] Spring 3 applicationContext-security-JDBC.xml has beans:bean not bean?

查看:201
本文介绍了Spring 3 applicationContext-security-JDBC.xml有bean:bean不是bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我在我的ApplicationContext中我必须使用beans:bean而不是bean和如何解决它。

 <?xml version =1.0encoding =UTF-8?> 

< beans:beans xmlns =http://www.springframework.org/schema/security
xmlns:beans =http://www.springframework.org/schema / beansxmlns: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 =trueuse-expressions =true>
< intercept-url pattern =/ friends / **access =hasRole('ROLE_USER')/>

< form-login login-page =/ login.html
default-target-url =/ index.htmlalways-use-default-target =
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 =locationvalue =classpath:jdbc.properties/>
< / beans:bean>


< beans:bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSource>
< beans:property name =driverClassNamevalue =$ {database.driver}/>
< bean:property name =urlvalue =$ {database.url}/>
< beans:property name =usernamevalue =$ {database.user}/>
< beans:property name =passwordvalue =$ {database.password}/>
< bean:property name =initialSizevalue =5/>
< bean:property name =maxActivevalue =10/>
< / beans:bean>




< / beans:beans>


解决方案

在这里处理XML命名空间。 Spring配置允许您使用来自不同命名空间的配置元素作为扩展基本 beans 命名空间配置的方式,使用方便的特定于域的配置,如上述情况下的安全配置。 / p>

如果你的配置文件集中在其中一个扩展名命名空间 - 让我们以安全为例 - 它可以清理文件如果你声明默认命名空间作为扩展命名空间,而不是标准的 beans 命名空间。这是什么

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

会使安全性成为默认命名空间,这意味着您不必在其前面加上 sec



但是当你使 security 默认值,那么当使用 beans 命名空间元素时,必须是显式的。因此, beans:前缀。



解决方案 c> beans 作为默认值,只需将默认名称空间更改为 beans

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


$ $ c> xmlns:b =http://www.springframework.org/schema/beans

而不是

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

这将允许您执行

 < b:bean id =beanIdclass =xyzBeanClass/> 


can someone please tell me what in my ApplicationContext I have to use beans:bean and not bean and how to fix it.

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

解决方案

Explanation. Basically you are dealing with XML namespaces here. Spring configuration allows you to use configuration elements from different namespaces as a way to extend the basic beans namespace configuration with convenient domain-specific configuration, like security configuration in the case above.

In cases where your configuration file concentrates on one of these extension namespaces--again, let's use security as an example--it can clean up the file if you declare the default namespace to be the extension namespace instead of the standard beans namespace. That's what

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

does--it makes security the default namespace, which means you don't have to prefix it with sec: or security:.

But when you make security the default, then you have to be explicit when using beans namespace elements. Hence the beans: prefix.

Solution. If you prefer beans to be the default, just change the default namespace to beans:

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

Alternative solution. Alternatively, if you want to type something shorter, you could do

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

instead of

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

which would allow you to do things like

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

这篇关于Spring 3 applicationContext-security-JDBC.xml有bean:bean不是bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆