在Spring XML配置中使用p和util命名空间的正确方法 [英] Correct way to utilize p and util namespace in Spring XML Configuration

查看:273
本文介绍了在Spring XML配置中使用p和util命名空间的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal is to rewrite the sessionFactory section of my xml file into the same format as all other areas in my xml file. I need to use the p-namespace to make things look consistent and neat. The problem that I ran into is using the util/p namespace.

感谢您让我编辑此帖子。这是我的整个xml文件:

Thank you for letting me edit this post. This is my entire xml file:

<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的组合:

This is what I have so far - using a combination of util:list and 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:属性:

The error message that I'm getting currently pertains to the util:list, but I'm equally suspicious of my util:properties as well:

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:属性的哪一部分必须更改才能使其生效?

What part of my util:list and util:properties must I change to get this to work?

推荐答案

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

What XML namespaces do p and util map to? These need to be declared with xmlns:p="..." and xmlns:util="..." somewhere within the XML element or a parent element of which they are being used.

(您收到的错误并非特定于SAX,而是XML的通用错误解析。)

(The error you're receiving is not specific to SAX, but is generic to XML parsing.)

例如,对于使用 util ,您的XML应该从以下开始:

For example, for using util, your XML should begin with the following:

<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

Additional details are available at http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util.

For p ,您还要添加:

For p, you'd also want to add:

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

请注意,没有任何要求你使用 p: util:。这些只是按惯例使用。您可以重写XML以使用 a: b:无处不在 - 只要它们被定义为映射到相同的XML名称空间。 (这就是他们需要定义的原因。)

Note that nothing requires you to use p: and util:. These are simply being used by convention. You could rewrite your XML to use a: and b: everywhere - as long as they are defined to map to the same XML namespaces. (This is why they need to be defined.)

这篇关于在Spring XML配置中使用p和util命名空间的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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