【问题标题】:Correct way to utilize p and util namespace in Spring XML Configuration在 Spring XML 配置中使用 p 和 util 命名空间的正确方法
【发布时间】:2012-02-10 15:39:32
【问题描述】:

我的目标是将我的 xml 文件的 sessionFactory 部分重写为与我的 xml 文件中的所有其他区域相同的格式。我需要使用 p-namespace 使事情看起来一致和整洁。我遇到的问题是使用 util/p 命名空间。

感谢您让我编辑这篇文章。这是我的整个 xml 文件:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<!-- DataSource Beans -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close"
    p:url="jdbc:hsqldb:file:database.dat;shutdown=true"
    p:driverClassName="org.hsqldb.jdbcDriver"
    p:username="sa"
    p:password="" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list>
            <value>/com/bookstore/domain/Book.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<!-- Template Beans -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
    p:dataSource-ref="dataSource" />

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"
    p:sessionFactory-ref="sessionFactory" />


<!-- DAO Beans -->
<bean id="bookDao" class="com.bookstore.data.BookDao"
    p:hibernateTemplate-ref="hibernateTemplate" />

<bean id="accountDao" class="com.bookstore.data.AccountDao"
    init-method="createTable"
    p:jdbcTemplate-ref="jdbcTemplate" />


<!-- Service Beans -->
<bean id="bookService" class="com.bookstore.services.BookService" 
    p:bookDao-ref="bookDao" />

<bean id="purchasingService" class="com.bookstore.services.PurchasingService"
    p:bookServiceInterface-ref="bookService" 
    p:accountServiceInterface-ref="accountService" ></bean>

<bean id="accountService" class="com.bookstore.services.AccountService"
    p:accountDao-ref="accountDao" />


<!-- AOP Advice Beans -->
<bean id="loggingAdvice" class="com.bookstore.advice.LoggingAdvice" />

<bean id="performanceTimingAdvice" class="com.bookstore.advice.PerformanceTimingAdvice" />

<!-- Auto Proxy -->
<aop:aspectj-autoproxy />

 </beans>

这是我目前所拥有的 - 使用 util:list 和 util:properties 的组合:

<util:list id="mappingResourcesList">
    <value>/com/bookstore/domain/Book.hbm.xml</value>
</util:list>

<util:properties id="hibernatePropertiesProps">
    <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
</util:properties>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
    p:dataSource-ref="dataSource"
    p:mappingResources-ref="mappingResourcesList"
    p:hibernateProperties-ref="hibernatePropertiesProps" />

我目前收到的错误消息与 util:list 相关,但我同样怀疑我的 util:properties:

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 
Line 22 in XML document from class path resource [application.xml] is invalid; 
nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: 
The matching wildcard is strict, but no declaration can be found for element 'util:list'.

我必须更改 util:list 和 util:properties 的哪一部分才能使其正常工作?

【问题讨论】:

    标签: java xml spring


    【解决方案1】:

    putil 映射到哪些 XML 命名空间?这些需要在 XML 元素或正在使用它们的父元素中的某处使用 xmlns:p="..."xmlns:util="..." 声明。

    (您收到的错误不是 SAX 特有的,而是 XML 解析的通用错误。)

    例如,对于使用util,您的 XML 应以以下内容开头:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    

    更多详情请访问http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util

    对于p,您还需要添加:

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

    请注意,您无需使用p:util:。这些只是按惯例使用。您可以重写您的 XML 以在任何地方使用 a:b: - 只要它们被定义为映射到相同的 XML 命名空间。 (这就是为什么需要定义它们。)

    【讨论】:

    【解决方案2】:

    xsi:schemaLocation 中缺少以下条目: http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util.xsd

    将它们放入您的 XML 中,您应该会很好。

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • @VirajNalawade 我不认为他是说要点击链接;我认为他实际上是在说 OP 需要将该 url 粘贴到 xml 文件中的特定位置(“...in xsi:schemaLocation..”,这是 OP 代码中第一个 xml 标记中的一个属性)。虽然答案很简洁,也许可以写得更清楚一点,但仅链接的答案不是。
    • @DennisMeng 没错.. 我认为至少在链接中添加更多关于标签的描述会更好..
    • ziesemer 的回答已经解决了这个问题。您的回答在 4 年后没有任何新的贡献。
    猜你喜欢
    • 2014-10-11
    • 2019-01-27
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多