xml configuration for Spring

Keywords: Attribute Spring xml

xml file configuration for spring

1. Assembling Bean Objects with xml

_Assemble in the applicationContext.xml file using the <bean></bean>tag.The class writes the full class name of the Bean object to be assembled, and the unique identity of the Bean object is written in the id.

<bean class="Full Class Name of Class" id="this bean Unique identification of object"></bean>


Description: The difference between id attribute and name attribute in <bean></bean>tag

The_id attribute is the unique identifier, and its value is the unique identifier number of the Bean object.The name property can set multiple aliases for the Bean object, each of which can be separated by a space or comma or semicolon; the bean object can be obtained from any alias.However, multiple Bean objects cannot have the same name attribute value.

<bean class="Full Class Name of Class" name="Identity 1 Identity 2..."></bean>

2. Dependent Injection Based on a Parameter Constructor

2.1 Dependent Injection of Complex Types

1) Injecting Bean objects by means of a parametric constructor

<contructor-arg ref="Object's id Attribute Value"></contructor-arg>


2) Dependency injection through c namespace

Introduce the following into the constraints of spring's configuration file:

 xmlns:c="http://www.springframework.org/schema/c"
<bean c:Parameter list name with parametric constructor-ref="Object's id attribute value"></bean>

<bean c:_parametric constructor parameter list index subscript-ref="object id attribute value"></bean>

2.2 Simple type of dependency injection

1) Dependent injection using a parametric constructor

<constructor-arg value="Attribute Value"></constructor-arg>

<constructor-arg name="Property Name" value="Attribute Value"></constructor-arg>

<constructor-arg index="Indexes" value="Attribute Value"></constructor-arg>


2) Dependency injection using c namespace

Introducing constraints in the constraint space of the spring configuration file

xmlns:c="http://www.springframework.org/schema/c"


3) Injection List Type

Injecting a simple type into the List collection

<list>
      <value>Value 1</value>
      <value>Value 2</value>
      ......
</list>

Injecting objects into List types

<list>
       <ref bean="Object 1" />
       <ref bean="Object 2" />
       ......
</list>

_In the List collection, if a simple type is injected, use <list><value></value</list>; if a non-simple type is injected, use <list><ref bean="/></list>

4) Injection Set Collection

Simple type injection:

<set>
    <value>Value 1</value>
    <value>Value 2</value>
      ......
</set>

Complex type injection:

<set>
    <ref bean="Object 1" />
     <ref bean="Object 2" />
      ......
</set>

The following is an example of assembling an object in a Set:

Note: The bottom level of the Set collection here is HashSet, which is an ordered Set collection that maintains the order of the elements in the collection, and the elements in the collection are not repeatable.

5) Injection map collection

Simple type of injection:

<map>
      <entry key="key1" value="Value 1" />
      <entry key="key2" value="Value 2" />
       ......
</map>

Injections of complex types:

<map>
      <entry key="key1" value-ref="Object 1" />
      <entry key="key2" value-ref="Object 2" />
       ......
</map>


6) Injection Array Type

Simple type of injection:

<array>
        <value>Value 1</value>
        <value>Value 2</value>
        ......
</array>

Injections of complex types:

<array>
       <ref bean="Object 1 id" />
        <ref bean="Object 2 id" />
        ......
</array>

3. Dependency Injection Based on setter Method

3.1 Simple type of assembly

<property name="Property Name" value="Attribute Value"></property>


Note: Make a distinction between member variables and attribute names.

_Member variables refer to variables or constants declared in a class; property names refer to getter/setter methods that remove the remainder of get/set and lower case the first letter, for example, setTrack ->Track ->track.

3.2 Assembly Complex Types

1) Assembling objects using setter methods

<property name="Property Name" ref="To be assembled bean Unique identification of object"></property>

2) Assemble arrays using the setter method

When an element is a simple type:

<array>
        <value>Value 1</value>
        <value>Value 2</value>
        ......
</array>

Elements are complex types:

<array>
       <ref bean="Object 1" />
       <ref bean="Object 2" />
       ......
</array>


3) Assemble List using setter method

Elements are simple types:

<list>
     <value>Value 1</value>
     <value>Value 2</value>
     ......
</list>

Elements are complex types:

<list>
     <ref bean="Object 1" />
     <ref bean="Object 2" />
     ......
</list>


4) Assembling a Map Collection using the setter method

Elements are simple types:

<map>
     <entry key="key1" value="Value 1" />
     <entry key="key2" value="Value 2" />
     ......
</map>

Elements are complex types:

<map>
      <entry  key="key1" value-ref="Object 1"/>
      <entry  key="key2" value-ref="Object 2"/>
      ......
</map>

3.3 Use p namespace injection

Declare the constraints of the p namespace in the spatial constraints of spring's xml configuration file:

 xmlns:p="http://www.springframework.org/schema/p"
<bean id="identifier" class="To create bean The full class name of the object's class" p:Property Name="Attribute Value" 
p:attribute-ref="bean Object Identifier"></bean>

3.4 Use util namespace injection

The setter method is used for injection of collection types such as List, Map, Set, and Array.

Using util namespaces requires the following constraints to be introduced:

  xmlns:util="http://www.springframework.org/schema/util"
  
 xsi:schemaLocation="http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd"


Applicable to arrays and list collections

<util:list id="identifier">
     <ref bean="Object 1" />
     <ref bean="Object 2" />
     ......
</util:list>


For Map Collections

<util:map id="identifier">
    <!--map Elements in a collection are simple types
    <entry key="key1" value="Value 1" />
    <entry key="key2" value="Value 2" />
    ......
    -->
    <!--map Elements in a collection are complex types
    <entry key="key1" value-ref="Object 1" />
    <entry key="key2" value-ref="Object 2" />
    ......
    -->
</util:map>

Applicable to set collection

<util:set id="identifier">
        <!--
         Elements in set collection are simple types
         <value>value 1</value>
         <value>value 2</value>
         ......
         -->
         
         <!--
         Elements in set collection are complex types
         <ref bean="Object 1"/>
         <ref bean="Object 2"/>
         ......
         -->
</util:set>

Posted by SQL Maestro on Sat, 17 Aug 2019 18:34:13 -0700