20

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.

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>

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" />

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'.

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

2 Answers 2

38

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.

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

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

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, you'd also want to add:

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

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.)

5
2

You have following entries missing in xsi:schemaLocation: http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd

Put them in your XML and you should be good.

4
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Dec 8, 2015 at 6:05
  • 1
    @VirajNalawade I don't think he's saying to follow the link; I think he's actually saying that the OP needs to paste that url in a specific location in the xml file ("...in xsi:schemaLocation..,", which is an attribute in the first xml tag in OP's code). While the answer is terse and maybe could be written to be a tad more clear, a link-only answer this is not. Dec 8, 2015 at 7:29
  • @DennisMeng thats right.. What I thought atleast adding more description about tags with the link would have better.. Dec 8, 2015 at 7:33
  • 1
    ziesemer's answer has already addressed this. Your answer does not contribute anything new 4 years later. Dec 8, 2015 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.