Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to utilize p and util namespace in Spring XML Configuration

Tags:

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?

like image 711
harryo Avatar asked Jan 15 '12 04:01

harryo


People also ask

Which is the correct syntax for using the P-namespace?

To enable the p-namespace feature, we need to add the xmlns:p="http://www.springframework.org/schema/p" into the XML file. Note that this namespace does not have a separate XSD file; therefore, IDEs such as IntelliJ do not recognize it.

What is the use of the P-namespace in Spring?

1.1 Spring P-namespace In spring, developers use the traditional <property> tag to set the properties of any bean. To avoid this, developers can use the p-namespace to perform the setter-based dependency injection. To use it, developers add a declaration in the spring configuration file i.e.

Which property is replaced by P-namespace in the Spring framework?

In Spring XML, p-namespace is the XML shortcut for <property> tag to inject bean dependency. The p-namespace replaces <property> tag in XML configuration.

What is namespace configuration in Spring?

Namespace configuration has been available since version 2.0 of the Spring framework. It allows you to supplement the traditional Spring beans application context syntax with elements from additional XML schema. You can find more information in the Spring Reference Documentation.

What is P-namespace in spring XML?

In Spring XML, p-namespace is the XML shortcut for <property> tag to inject bean dependency. The p-namespace replaces <property> tag in XML configuration. It is easier and clear to use and increases readability of XML configuration. Suppose we have <bean> definition in XML as follows.

What XML namespaces do P and util map to?

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

How to change <property> tag in Spring Boot using p-namespace?

We can change <property> tag using p-namespace as follows. Find the XML file with p-namespace . Find the bean classes used in the example. Run Application. Find the output. In the same way, for constructor-arg tag, Spring provides c-namespace shortcut.

Are the objects defined in a spring XML configuration file generic?

The objects defined in a Spring XML configuration file are not all generic, vanilla beans. Usually, each bean requires some degree of specific configuration. Spring 2.0’s new XML Schema-based configuration addresses this issue.


2 Answers

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

like image 98
ziesemer Avatar answered Nov 15 '22 22:11

ziesemer


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.

like image 39
user5652931 Avatar answered Nov 16 '22 00:11

user5652931