Spring中IoC操作Bean管理(基于xml方法)


前言:
Bean管理是指Spring创建对象和Spring注入属性;
Bean管理操作有两种方式:基于xml配置文件方式实现和基于注解方式实现。
本篇文章先讲基于xml配置文件方式实现。

一.基于 xml 方式创建对象

首先创建一个Person类,如下:

public class Person {
    
    
    private String name;
    private int age;
    private String gender;

    public void eat(){
    
    
        System.out.println("吃饭!");
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public String getGender() {
    
    
        return gender;
    }

    public void setGender(String gender) {
    
    
        this.gender = gender;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

  1. 在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建
  2. 在 bean 标签有很多属性,介绍常用的属性
    • id 属性:唯一标识
    • class 属性:类全路径(包类路径)
  3. 创建对象时候,默认也是执行无参数构造方法完成对象创建

xml配置中添加如下代码,完成对象创建:

<bean id="person" class="iocbean.byxml.Person"></bean>

二.基于 xml 方式注入属性

DI:依赖注入,就是注入属性
分为两种注入方式:

  1. set 方法进行注入
  2. 有参数构造进行注入

1.使用 set 方法进行注入

<bean id="person" class="iocbean.byxml.Person">
    <!--set 方法注入属性-->
    <!--使用 property 完成属性注入
        name:类里面属性名称
        value:向属性注入的值
    -->
    <property name="name" value="小明"></property>
    <property name="age" value="21"></property>
    <property name="gender" value=""></property>
</bean>

2.使用有参数构造进行注入

首先在刚才创建的Person类中添加有参构造器

    public Person(String name, int age, String gender) {
    
    
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

然后在 spring 配置文件配置对象创建,配置属性注入

    <bean id="person1" class="iocbean.byxml.Person">
        <!--有参数构造注入属性-->
        <!--使用 constructor-arg 完成属性注入
            name:类里面属性名称
            value:向属性注入的值
        -->
        <constructor-arg name="name" value="小红"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="gender" value=""></constructor-arg>
    </bean>

3.p名称空间注入(只做了解)

使用p名称空间注入,可以简化基于xml的配置方式
首先我们先添加p名称空间在配置文件中
在这里插入图片描述
然后进行属性注入,在bean标签里面进行操作

    <!-- set方法注入属性
         P:xxx 其中xxx为类里面的属性名称
         例如:p:name="小华" 其中name为Person类的属性
    -->
    <bean id="person2" class="iocbean.byxml.Person" p:name="小华" p:age="20" p:gender=""></bean>

三.xml注入其他类型属性

1.字面量

  1. null值
        <property name="name">
            <null/>
        </property>
  1. 属性值包含特殊符号
    把带特殊符号内容写到 CDATA内
        <property name="gender">
            <value><![CDATA[<男>]]></value>
        </property>

2.注入属性(外部bean)

在已有上述Person类的情况下,再创建一个Student类,在Student里有类型为Person的属性,Student类如下:

public class Student {
    
    
    private Person person;
    private String university;

    public Student() {
    
    
    }

    public Student(Person person, String university) {
    
    
    
        this.person = person;
        this.university = university;
    }

    public Person getPerson() {
    
    
        return person;
    }

    public void setPerson(Person person) {
    
    
        this.person = person;
    }

    public String getUniversity() {
    
    
        return university;
    }

    public void setUniversity(String university) {
    
    
        this.university = university;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "person=" + person +
                ", university='" + university + '\'' +
                '}';
    }
}

在配置文件中进行配置:

    <bean id="student0" class="iocbean.byxml.Student">
        <!--注入 Person 对象
        name 属性:类里面属性名称
        ref 属性:创建 Person 对象 bean 标签 id 值 -->
        <property name="person" ref="person1"></property>
        <property name="university" value="Yangtze University"></property>
    </bean>

    <bean id="person1" class="iocbean.byxml.Person"></bean>

3.注入属性(内部bean)

    <bean id="student1" class="iocbean.byxml.Student">
        <!--设置一个普通属性-->
        <property name="university" value="Yangtze University"></property>
        <!--设置对象类型属性-->
        <property name="person">
            <bean id="person1" class="iocbean.byxml.Person">
                <property name="name">
                    <value><![CDATA[<小>]]></value>
                </property>
            </bean>
        </property>
    </bean>

4.注入属性(级联赋值)

    <!--级联赋值-->
    <bean id="student2" class="iocbean.byxml.Student">
        <property name="university" value="Yangtze University"></property>
        
        <property name="person" ref="person0"></property>
        <property name="person.name" value="改名了"></property>
    </bean>

    <bean id="person0" class="iocbean.byxml.Person">
        <!--set 方法注入属性-->
        <!--使用 property 完成属性注入
            name:类里面属性名称
            value:向属性注入的值
        -->
        <property name="name" value="小明"></property>
        <property name="age" value="21"></property>
        <property name="gender" value=""></property>
    </bean>

四.xml 注入集合属性

1.注入数组、list、map、set 类型属性

创建CollectionDemo类,如下:

public class CollectionDemo {
    
    
    private String[] name;
    private List<String> list;
    private Map<String,String> maps;
    private Set<String> sets;

    public void setName(String[] name) {
    
    
        this.name = name;
    }

    public void setList(List<String> list) {
    
    
        this.list = list;
    }

    public void setMaps(Map<String, String> maps) {
    
    
        this.maps = maps;
    }

    public void setSets(Set<String> sets) {
    
    
        this.sets = sets;
    }

    @Override
    public String toString() {
    
    
        return "CollectionDemo{" +
                "name=" + Arrays.toString(name) +
                ", list=" + list +
                ", maps=" + maps +
                ", sets=" + sets +
                '}';
    }
}

配置文件如下:

<?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="collectionDemo" class="iocbean.byxml.collection.CollectionDemo">
        <!--数组类型属性注入-->
        <property name="name">
            <array>
                <value>小明</value>
                <value>小华</value>
                <value>小红</value>
            </array>
        </property>

        <!--list 类型属性注入-->
        <property name="list">
            <list>
                <value>123</value>
                <value>456</value>
                <value>789</value>
            </list>
        </property>

        <!--map 类型属性注入-->
        <property name="maps">
            <map>
                <entry key="数学" value="mathematics"></entry>
                <entry key="物理" value="physics"></entry>
                <entry key="生物" value="biology"></entry>
            </map>
        </property>

        <!--set 类型属性注入-->
        <property name="sets">
            <set>
                <value>150</value>
                <value>110</value>
                <value>90</value>
            </set>
        </property>
    </bean>
</beans>

2.在集合里面设置对象类型值

    <!--创建多个 course 对象-->
    <bean id="student0" class="iocbean.byxml.collection.Student">
        <property name="name" value="小红"></property>
    </bean>
    <bean id="student1" class="iocbean.byxml.collection.Student">
        <property name="name" value="小华"></property>
    </bean>
    <bean id="student2" class="iocbean.byxml.collection.Student">
        <property name="name" value="小明"></property>
    </bean>
    
    <!--注入 list 集合类型,值是对象-->
    <bean id="collectionDemo1" class="iocbean.byxml.collection.CollectionDemo1">
        <property name="studnets">
            <list>
                <ref bean="student0"></ref>
                <ref bean="student1"></ref>
                <ref bean="student2"></ref>
            </list>
        </property>
    </bean>

3.把集合注入部分提取出来

  1. 在Spring配置文件中引入名称空间util
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
  1. 使用 util 标签完成 list 集合注入提取
    <!--1 提取 list 集合类型属性注入-->
    <util:list id="studentsList">
        <ref bean="student0"></ref>
        <ref bean="student1"></ref>
        <ref bean="student2"></ref>
    </util:list>
    <!--2 提取 list 集合类型属性注入使用-->
    <bean id="collectionDemo2" class="iocbean.byxml.collection.CollectionDemo1">
        <property name="studnets" ref="studentsList"></property>
    </bean>

猜你喜欢

转载自blog.csdn.net/MrYushiwen/article/details/110674649