Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • How to use p-Namespace

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 544
    Comment on it

    In Spring based applications, p-NameSpace (Parameter Namespace) provides a substitute for Property tag. With the help of "p-Namespce", it helps to inject the dependency Injection by allowing direct to use bean tag irrespective of property tag.
    The benefits of using p-namespce are as follows :

    • Using "p" as tag is more compact than property tag
    • Simplifies the setter-based dependency injection code
    • Using p namespace reduces the amount of XML required in Spring configuration

    In order to use p-namespace follow the following steps:

    1. In the configuration xml file, add "p" namespace URI like `"xmlns:p="http://www.springframework.org/schema/p"
    2. For each property tag add an attribute p:<property-name>value and set the attribute value to it
    3. If one of your property is expecting another bean , then you should suffix -ref to your property name

    for ex:

    <xmp>
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="addr1" class="com.pfl.samples.spring.cpns.Address">
            <property name="houseNo" value="2106"/>
            <property name="street" value="Newbury St"/>
            <property name="city" value="Boston"/>
            <property name="zipCode" value="02115-2703"/>
        </bean>
    
        <bean id="constArgAnotherBean" class="com.pfl.samples.spring.cpns.Person">
            <property name="firstName" value="Joe"/>
            <property name="lastName" value="John"/>
            <property name="age" value="35"/>
            <property name="address" ref="addr1"/>
        </bean>
    
    </beans> 
    </xmp>
    

    So using p-namespace we can write like this :

    <xmp>
    <?xml version="1.0" encoding="UTF-8"?>
    <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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean name="addr1" class="com.pfl.samples.spring.cpns.Address"
            p:houseNo="2106" p:street="Newbury St"
            p:city="Boston" p:zipCode="02115-2703"/>
        <bean name="person1" class="com.pfl.samples.spring.cpns.Person"
            p:firstName="Joe" p:lastName="John"
            p:age="35" p:address-ref="addr1"/>
    
    </beans>
    </xmp>
    
    Spring

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: