Detailed explanation of Spring Xml assembling Bean

  • 2021-12-09 08:44:31
  • OfStack

1. Overview

There are three ways to configure Bean in Spring:

Configure the discovery mechanism and automatic assembly principle of implicit Bean in the interface and implementation class of Java in xml file
These three methods are often used, and they are often mixed. This article first writes xml assembly Bean.

2. Analyze bean tags


<bean id="pserson" class="com.xl.spring.xlIoc.beans.Person">
    <property name="id" value="1"/>
    <property name="name" value=" Song Ji Hyo "/>
    <property name="nickName" value=" Meng Meng "/>
</bean>

1. id: Give bean a globally unique name. This attribute is optional. If this attribute is not declared, Spring will automatically generate 1 number in the way of "fully qualified name # {number}", and number will count from 0.
For example, two Person objects are declared, as follows:


<bean class="com.xl.spring.xlIoc.beans.Person">
    <property name="id" value="1"/>
    <property name="name" value=" Song Ji Hyo "/>
    <property name="nickName" value=" Meng Meng "/>
</bean>
<bean class="com.xl.spring.xlIoc.beans.Person">
    <property name="id" value="2"/>
    <property name="name" value=" Jay Chou "/>
    <property name="nickName" value="Jack"/>
</bean>

At this time, you want to get the object:


ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml");
Person person = (Person) ac.getBean(Person.class.getName() + "#1");
System.out.println(person.toString());

2. class: The type of the injected object, corresponding to the fully qualified name of the class.
3. property: Defines the properties of the class, where name represents the property name and value represents the property value.

3. Assembly set

The above implementation injection is for 1 basic data type and String type. If the data type is a collection, do the following:
1. First define a new class > Coder:


package com.xl.spring.xlIoc.beans;
 
import lombok.Data;
 
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
 
@Data
public class Coder {
    private String name;
    private List<String> languages;
    private Set<String> friends;
    private Map<String, Integer> houses;
    private Properties properties;
}

2. In the xml file of Spring, inject Coder:


<bean id="coder" class="com.xl.spring.xlIoc.beans.Coder">
    <property name="name" value=" Song Ji Hyo "/>
    <!-- Configure list Set -->
    <property name="languages">
        <list>
            <value>java</value>
            <value>JavaScript</value>
            <value>SQL</value>
        </list>
    </property>
    <!-- Configure set Set -->
    <property name="friends">
        <set>
            <value> Kim Jong Kook </value>
            <value> Hedong Hoon </value>
            <value> Liu Zaishi </value>
            <value> Ishiku Chi </value>
            <value> Quan Shao Min </value>
        </set>
    </property>
    <!-- Configure map Set -->
    <property name="houses">
        <map>
            <entry key=" Mapu District " value="240000000" />
        </map>
    </property>
    <!-- Configure properties Set -->
    <property name="properties">
        <props>
            <prop key=" Height ">168</prop>
            <prop key=" Weight ">52.3</prop>
            <prop key=" Age ">30</prop>
        </props>
    </property>
</bean>

3. Call:


public static void testCoder() {
    //1. Obtained through the configuration file spring Context of 
    ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml");
    Coder coder = (Coder) ac.getBean("coder");
    coder.getFriends().forEach(s -> System.out.println("friend ---> " + s));
}

4. Complex Bean assembly

If the property is a complex collection object, for example, the property is List or Map, and the generic type of this List is 1 object, or key and value of Map are both 1 object.
What should I do at this time?
Solution: First, inject Bean corresponding to generics, and then introduce the past when injecting attributes.
1. First define three classes: User objects and Role objects are required in UserRole


package com.xl.spring.xlIoc.beans;
 
import lombok.Data;
 
@Data
public class User {
    private Integer id;
    private String name;
}

package com.xl.spring.xlIoc.beans;
 
import lombok.Data;
 
@Data
public class Role {
    private Integer id;
    private String name;
}
 
 
package com.xl.spring.xlIoc.beans;
 
import lombok.Data;
 
import java.util.List;
import java.util.Map;
 
@Data
public class UserRole {
    private Integer id;
    private List<User> users;
    private Map<Role, User> map;
}

2. Inject the corresponding User object and Role object first, then UserRole object:


<bean id="user1" class="com.xl.spring.xlIoc.beans.User">
    <property name="id" value="1"/>
    <property name="name" value=" Zhang Sheng "/>
</bean>
<bean id="user2" class="com.xl.spring.xlIoc.beans.User">
    <property name="id" value="2"/>
    <property name="name" value=" Yingying "/>
</bean>
<bean id="role1" class="com.xl.spring.xlIoc.beans.Role">
    <property name="id" value="1"/>
    <property name="name" value=" Administrator "/>
</bean>
<bean id="role2" class="com.xl.spring.xlIoc.beans.Role">
    <property name="id" value="2"/>
    <property name="name" value=" Ordinary users "/>
</bean>
<bean id="userRole" class="com.xl.spring.xlIoc.beans.UserRole">
    <property name="id" value="1"/>
    <property name="users">
        <list>
            <ref bean="user1"/>
            <ref bean="user2"/>
        </list>
    </property>
    <property name="map">
        <map>
            <entry key-ref="role1" value-ref="user1"/>
            <entry key-ref="role2" value-ref="user2"/>
        </map>
    </property>
</bean>

3. Create an object and use:


public static void testComplexBean() {
    ApplicationContext ac = new ClassPathXmlApplicationContext("springioc.xml");
    UserRole userRole = (UserRole) ac.getBean("userRole");
    System.out.println(userRole);
}

Related articles: