spring xml configuration

How to use p-namespace in Spring XML configuration for specifying a property argument concisely?

This tutorial explains how to use p-namespace in Spring XML configuration. As you know we can define bean properties and constructor arguments as references to other managed beans. Also Spring XML based configuration-metadata supports both <property/> and <constructor-arg/> as sub element types for this purpose.

<property/> Example:

The below example shows how to configure property element with value attribute which specifies a property using string representation. The conversion service API of Spring is responsible for converting these values from String to actual type of the property.

XML Config 1

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />

    <property name="url" value="jdbc:mysql://localhost:3306/sneppetsdb" />

    <property name="username" value="root" />

    <property name="password" value="password" />

</bean>

What is p-namespace ?

The p-namespace will allow you to use the bean element’s attributes instead of using <property/> sub elements in order to configure property values concisely. Spring supports extensible configuration formats using namespaces, which are based on XML schema definition. However note that p-namespace is not defined in an XSD and exists as part of Spring Core.

p-namespace in Spring XML configuration

The following example shows how to use p-namespace instead of property sub element to write same XML config 1 which is above.

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/sneppetsdb"
        p:username="root"
        p:password="password"/>

</beans>

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments