Using XML configuration bean_spring in spring

Source: Internet
Author: User
Tags array definition cdata

The Bean,bean attribute ID in the Applicationcontext.xml file using the Bean node configuration must be unique within the IOC container.

<bean id= "HelloWorld" class= "Com.test.spring.beans.HelloWorld" >
        <property "name" Name= "Spring" ></property>
</bean>

Two, three methods of dependency injection method of attribute injection construction injection into the factory

(1) Attribute injection

Inject the bean's property values or dependent objects through the setter method. Property injection uses the element, using the Name property to specify the Bean's property name, the Value property, or the child node to specify the property value. Attribute injection is the most commonly used injection method in practical applications. The SetName () method in the HelloWorld class corresponds to the name attribute in the top code, for example, to change the SetName () method name to SetName2 () and an error occurs when the Name property value is name. You need to change the Name property to Name2.

(2) Construction method injection

The constructor injects the property value of the bean or the dependent object, which guarantees that the bean instance can be used after it is instantiated.
Constructor injection declares a property in the <constructor-arg> element, and there is no Name attribute in <constructor-arg>. Assign a value to a property using either the value attribute or the values child node. You can use both the index index and the Type property to assign a value to which property. The value of index represents the position of the parameter in the constructor. Type represents the types of member properties, such as type= "double" or type= "java.lang.String"

<bean id= "Car" class= "Com.test.spring.beans.Car" >
        <constructor-arg value= "Audi" index= "0" ></ constructor-arg>
        <constructor-arg value= "Shanghai" index= "1" ></constructor-arg>
        < Constructor-arg value= "300000" type= "double" ></constructor-arg>
</bean>

If the injected value has a special character Furu "<>", use <! [cdata[]]> wrapped up, not wrapped up in the XML will be an error. Such as:

        <constructor-arg type= "java.lang.String" >
            <value><![ Cdata[<shanghai^>]]></value>
        </constructor-arg>

The console prints a value of <shanghai^>

(1) When injecting a property into a bean, if it contains other beans, you can specify a reference to the bean by the element or ref property for the Bean's property or constructor parameter.
(2) A bean's declaration can also be included in a property or constructor, which is called an internal bean and can be declared as an internal bean when the bean instance is used only for a particular property. The internal Bean Declaration is directly contained in the <property> or <constructor-arg> element, and no ID or name attribute needs to be set, and the internal bean cannot be used anywhere else.

</bean> <!--use constructor injection property value to specify the position of the parameter and the type of the parameter to distinguish the overloaded constructor constructor injection in the <constructor-arg> element to declare the property--> <bean id= "Car2" class= "Com.test.spring.beans.Car" > <constructor-arg value= "baoma" type= "Java.lang.Stri" Ng "></constructor-arg> <!--if the literal contains special characters, you can use the <! 
            [cdate[]]> wrapped up--> <!--injection property values can also use the Value child label--> <constructor-arg type= "java.lang.String" > <value><!
            [cdata[<shanghai^>]]></value> </constructor-arg> <constructor-arg type= "int" > <value>250</value> </constructor-arg> </bean> <bean id= "Persion" class= "Com.test.spring.beans.Persion" > <property name= "name" value= "Tom" ></property> &LT;PR 
        Operty name= "Age" value= ></property> <!--You can use the property's ref attribute to establish a reference relationship between beans--> <!-- <property name= "Car" ref= "Car2" ></property>--> <!--<property name= "car" ref= "car2" > <ref bean = "Car2"/> </property>--> <!--internal Bean, cannot be referenced externally, can only be used internally--> <property Name= "Car" > <bean class= "Com.test.spring.beans.Car" > <constructor-arg value= "Ford"
                ></constructor-arg> <constructor-arg value= "Changan" ></constructor-arg> <constructor-arg value= "200000" type= "double" ></constructor-arg> </bean> </pro perty> <property name= "car.maxspeed" value= "></property> </bean>"

You can inject null values into the properties of a Bean's string or other object type using a private <null/> element label.
Like Struts, Hiberante and other frameworks, Spring supports the configuration of cascading properties. However, the attribute needs to be initialized first. For example, a bean in code that requires a car object to be configured

<bean id= "Persion2" class= "com.test.spring.beans.Persion" >
        <constructor-arg value= "Jerry" ></ constructor-arg>
        <constructor-arg value= "></constructor-arg>
        <!--
         < Constructor-arg ref= "Car" ></constructor-arg>
         -->
        <!--test assignment null-->
        <!--  
        < constructor-arg><null/></constructor-arg>
        -->
        <constructor-arg ref= "Car" ></ Constructor-arg>
        <!--assign values to cascading properties, note that properties need to be initialized before they can be assigned to a cascading property, otherwise there will be exceptions, unlike struts2. -->
        <property name= "car.maxspeed" value= "></property> </bean>"
 

Third, collection properties

When the injected property is a collection, spring also provides a set of built-in XML tags (for example: <list>, <set>, or <map>) to configure the collection properties.
To configure the properties of the Java.util.List type, you need to specify the <list> tag and include some elements in the tag. These tags can specify a simple constant value by <value> specifying a reference to another Bean. Specify the built-in bean definition by <bean>. By specifying an empty element. You can even embed other collections.
The array definition is the same as the list, using the <list> configuration Java.util.Set need to use the <set> tag, the method of defining the element is the same as the list.

   <!--test How to Configure collection Properties-->
     <bean id= "Persion3" class= "com.test.spring.beans.collection.Persion" >
        < Property name= ' name ' value= ' Mike ' ></property>
        <property name= ' age ' value= ' ' ></property>
        <property name= "Cars" >
        <!--Use the list node to assign values to the properties of the list type-->
            <list>
                <ref bean= "Car"/ >
                <ref bean= "car2"/>
            </list>     
        </property>
     </bean>

Java.util.Map through the <map> tag definition, <map> tags can use multiple <entry> as a child label. Each entry contains a key and a value. Keys must be defined in the <key> tag, because the types of keys and values are not limited, so you are free to specify <value>, <ref>, <bean> or <null> elements. The
can use the key and value of a Map as a property definition for <entry>: A simple constant is defined using key and value; The Bean reference is defined by the Key-ref and Value-ref properties.
Use the <props> definition java.util.Properties, which uses more than one <prop> as a child label. Each <prop> tag must define the key attribute.

<!--Configure Map property value--> <bean id= "newpersion" class= "Com.test.spring.beans.collection.NewPersion" >
        ;p roperty name= "name" value= "Rose" ></property> <property name= "age" value= "a" ></property>
                <property name= "Cars" > <!--Configure the member variables of the map type using the map node and the entry child nodes of the map--> <map> <entry key= "AA" value-ref= "car" ></entry> <entry key= "BB" value-ref= "Car2" 
     ;</entry> </map> </property> </bean> <!--Configuring Properties property Value--> <bean id= "DataSource" class= "Com.test.spring.beans.collection.DataSource" > <property name= "Properti" Es "> <!--use props and prop subnodes to assign values to properties--> <props> <prop key=" u Ser ">root</prop> <prop key=" password ">123456</prop> <prop key=" Jdb CUrl ">jdbc:mysql:///test</prop> <prop key= "Driverclass" >com.mysql.jdbc.Driver</prop> &LT;/PROPS&G
        T </property> </bean>

When you define a collection using a Basic collection label, you cannot share a collection with different beans because the collection cannot be defined as a separate bean, causing other beans to not be able to reference the collection.
You can use the collection label in the Util schema to define a separate collection Bean. It is important to note that the Util schema definition must be added to the <beans> root element.

     <!--Configure a stand-alone collection bean to be referenced by multiple beans, import the Util namespace-->
     <util:list id= "Cars" > <ref bean=
        "Car"/>
        <ref bean= "car2"/>
     </util:list>

     <bean id= "Persion4" Com.test.spring.beans.collection.Persion ">
        <property name=" name "value=" Jack "></property>
        <property name= "age" value= "I" ></property>
        <property name= "Cars" ref= "Cars" ></ Property>
     </bean>

Iv. using the P-namespace

In order to simplify the configuration of XML files, more and more XML files use attributes rather than child element configuration information.
Spring has introduced a new P namespace starting with version 2.5, which allows you to configure the properties of the bean in the way of the <bean> element properties. After using the P namespace, the xml-based configuration is further simplified.

 <!--assign values to bean properties through the P namespace, you need to import the P namespace first, more concise than traditional configuration--> <bean id= "Persion5 class=" com.test.spring.be Ans.collection.Persion "P:name=" Queen "p:age=" p:cars-ref= "Cars" > </bean> 

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.