spring-injection parameters in detail-[simplified configuration method]

Source: Internet
Author: User
Tags constructor xmlns

Overview Literal property value literal property constructor parameter collection element Reference object property literal Value property constructor parameter collection element use P namespace reference literal property values through p namespace reference other beans by P namespace

Overview

Spring provides a simplified configuration for literals, reference beans, and collections, and can be used in a simplified configuration if no special features are used in full format. Literal property value Literal attributes

<?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= "Plane" class= "Com.xgj.ioc.inject.set.Plane" > <!--<property name= "Brand" > <value>Airbus&amp; a380</value> </property> <property name= "Color" > <value>red</value > </property> <property name= "Speed" > <value>700</value> & Lt;/property>-<!--simplified configuration--<property name= "brand" value= "airbus&amp;
    A380 "/> <property name=" color "value=" red "/> <property name=" Speed "value=" ></property> " </bean> </beans> 

Using a simplified method, you cannot use constructor parameters

<?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= "Tank" class= "Com.xgj.ioc.inject.construct.type.Tank" >

        <!-- 
        <constructor-arg type= " Java.lang.String ">
            <value>T72</value>
        </constructor-arg>

        <constructor-arg Type= "Double" >
            <value>20000.00</value>
        </constructor-arg>--

         < !--Simplified Way-
         <constructor-arg type= "java.lang.String"  value= "T72"/>
         <constructor-arg  type= "Double" value= "20000.00"/>

    </bean>
</beans>
Collection Elements
<?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=" Pets "class=" Com.xgj.ioc.inject.construct.jihe.strongType.Pets "> <property name=" Map "> <!--& Lt;map> <entry> <key> provides a value for integer, and when spring sets the value, it
                    Convert to defined integer type <value>111</value> </key> <value>cat</value> </entry> <entry> <key > <value>113</value> </key> <value
      >bird</value> </entry>          <entry> <key> <value>115</value> </key> <value>dog</value> </entry> </m Ap>-<!--simplified-<map> <entry key= "111" value= "C
            At "/> <entry key=" 113 "value=" Bird "/> <entry key=" $ "value=" dog "/> </map> </property> </bean> <bean id= "PetShop" class= "com.xgj.ioc.inject.constr
 Uct.jihe.strongType.PetShop "> <property name=" Pets "ref=" pets "/> </bean> </beans>
Referencing Object Properties Literal attributes

A complex way

<property name= "Plane" >
    <ref bean= "plane"/>
</property>

A simplified approach

<property name= "plane" ref= "plane"/>
constructor Parameters

A complex way

<constructor-arg>
    <ref bean= "plane" >
</constructor-arg>

A simplified approach

<constructor-arg ref= "Plane" >
Collection Elements

A complex way

<map>
    <entry>
        <key>
            <ref bean= "Keybean"/>
        </key>
        <ref bean= "Valuebean"/>
    </entry>
</map>

A simplified approach

<map>
    <entry key-ref= "Keybena" value-ref= "Valuebean"/>
</map>

The simplified form of the ref label corresponds to <ref bean= "xxx" > <ref local= "xxx" > and <ref parent= "xxx" > No corresponding simplified form using the P-namespace

The new P-namespace is introduced after the Spring2.5 version, and the Bean's properties can be configured through the bean element properties.

With the P namespace, XML-based configuration is further simplified.

Need to introduce

xmlns:p= "http://www.springframework.org/schema/p"
referencing literal property values through the P-namespace
<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns= "Http://www.springframework.org/schema/beans"
    xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
    introduces the P-namespace
    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 ">


    <!--use P namespace--
    <bean id=" Plane " class= "Com.xgj.ioc.inject.set.Plane"
        p:brand= "airbus&amp; A380 "
        p:color=" Red "
        p:speed="
    >
    </bean>

</beans>
referencing other beans through the P-namespace
<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns= "Http://www.springframework.org/schema/beans"
    xmlns:p= "http://www.springframework.org/schema/p"
    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=" police "class=" Com.xgj.ioc.inject.construct.ref.Police ">
        <property name=" Gun ">
             by using the gun in the REF application container, Build police and gun dependencies 
            <ref bean= "gun"/>
        </property> </bean>-

     <!-- Use the P namespace to  refer to other bean-->
    <bean id= "police" class= "Com.xgj.ioc.inject.construct.ref.Police"
        p: Gun-ref= "Gun"/>

    <bean id= "gun" class= "Com.xgj.ioc.inject.construct.ref.Gun"/>
</beans>

,<bean> use <property> child elements to configure bean properties before the P namespace is used.
With the P namespace, the attributes of the bean are configured with the element attributes of <bean>.

For literal attributes, the format is

p:< property name >-ref= "XXX"

The property of the referenced object, in the form of:

p:< property name >-ref= "XXX"

Because property names in the P namespace are mutable, there is no schema definition file for the P namespace and no schema definition file for the P namespace in xsi:schemalocation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.