Spring IOC—基于XML配置和管理Bean 万字详解(通俗易懂)

目录

一、前言

二、通过类型来获取Bean

        0.总述(重要) : 

        1.基本介绍 : 

        2.应用实例 : 

三、通过指定构造器为Bean注入属性

        1.基本介绍 : 

        2.应用实例 : 

四、通过p命名空间为Bean注入属性

        1.基本介绍 : 

        2.应用实例 : 

五、通过ref引用实现Bean的相互引用

        1.基本介绍 : 

        2.应用实例 : 

六、对Bean注入属性的内容延伸

        1.准备工作 : 

        2.注入List类型的属性 : 

        3.注入Set类型的属性 : 

        4.注入Map类型的属性 : 

        5.注入数组类型的属性 : 

        6.注入Properties类型的属性 : 

        7.List属性注入之通过util命名空间注入 : 

        8.级联属性注入 : 

七、通过静态工厂获取Bean

        1.基本介绍 : 

        2.应用实例 : 

八、通过实例工厂获取Bean

        1.基本介绍 : 

        2.应用实例 : 

九、通过FactoryBean获取Bean

        1.基本介绍 : 

        2.应用实例 : 

十、关于Bean配置的更多内容和细节

十一、总结


一、前言

  • 第二节内容,up打算和大家分享一下Spring IOC——基于XML方式对Bean的配置和管理。(PS: 若对XML文件未曾了解,可以去快速阅读一下up的“速通XML”一文。)
  • 注意事项——代码中的注释也很重要;不要眼高手低,自己跟着过一遍才有收获;点击文章的侧边栏目录或者文章开头的目录可以进行跳转。
  • 良工不示人以朴,所有文章都会适时补充完善。大家如果有问题都可以在评论区进行交流或者私信up。感谢阅读!

二、通过类型来获取Bean

        0.总述(重要) : 

        (1) Spring对Bean的管理主要包括两方面内容——创建bean对象为bean注入属性

        (2) Spring对Bean的配置方式也主要是两种——基于XML文件配置的方式基于注解配置的方式

        (3) 上一小节中,我们已经演示过“通过id属性来获取bean对象”的方式(基于beans.xml配置文件),因此这一小节,我们直接演示其他方式(这一小节均是基于XML文件配置的方式)。

        1.基本介绍 : 

        (1) 通过类型来获取bean对象,其实是调用了getBean(Class<T> aClass)方法,该方法与我们上一小节中演示的“通过id获取bean对象”的方法构成重载,如下图所示 : 

        (2) 通过类型来获取bean时,要求ioc容器中同一类型的bean对象只能有一个,否则会抛出异常NoUniqueBeanDefinitionException(不唯一Bean定义异常,如下图所示 : 

        (3) “通过类型获取Bean”方式的适用场景——在一个线程中只需要一个对象实例(单例)的情况,eg : XxxAction / Servlet / Controller / XxxService。

        (4) PS : 在容器配置文件(eg : beans.xml)中给bean对象的属性赋值,底层是通过setter方法完成的,因此我们定义的JavaBean(比如之前的Student)类中,必须提供相应的setter方法,否则报错。

        2.应用实例 : 

                需求 : 在beans.xml配置文件中配置一个bean对象,并通过Class类型的方式来获取到该bean对象。
                首先,我们需要创建一个JavaBean类,以Student类为例,Student类代码如下 : 

package com.cyan.spring.bean;

/**
 * @author : Cyan_RA9
 * @version : 21.0
 */
public class Student {
    private String name;
    private int age;
    private int score;

    //PS : 无参构造器必须给出,因为底层要通过反射来创建对象。
    public Student() {

    }
    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    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 int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }

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

                然后,在beans.xml配置文件中,配置一个Student类对象,beans.xml代码如下 : 

<?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">

    <!--
        (1) 在根元素beans中,通过<bean></bean>子元素来配置JavaBean对象。
            每配置一个bean,相当于配置了一个Java对象。
        (2) bean子元素需要配置两个属性———class 和 id。其中,
            class表示所要实例化类的正名(全类名);
            id表示该对象在Spring容器中的标识,通过id可以获取到对象。
        (3) property子元素用于配置该对象的成员变量(对象的属性),其中,
            name表示属性名称,value表示属性的值。
        (4) XML内容回顾———若一个标签没有标签体,以<age></age>为例,可以简写为<age/>。
    -->

    <!-- 注意———此处我们没有为bean标签配置id属性,默认的分配id为:全类名#0 -->
    <bean class="com.cyan.spring.bean.Student">
        <property name="name" value="Rain"></property>
        <property name="age" value="19"></property>
        <property name="score" value="439"></property>
    </bean>
</beans>

                接着,在测试类中调用getBean(Class<T> aClass)方法,以StudentBeanByXML类为测试类,通过JUnit框架进行单元测试,StudentBeanByXML类代码如下 : 

package com.cyan.spring.test;

import com.cyan.spring.bean.Student;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author : Cyan_RA9
 * @version : 21.0
 */
public class StudentBeanByXML {
    //1.通过类型来获取Bean
    @Test
    public void getBeanByClass() {
        //(1) 获取Spring容器对象
            //IOC : Inversion Of Control
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        //(2) 通过JavaBean的类型来获取bean
        Student bean = ioc.getBean(Student.class);

        //(3) 打印出获取到的bean对象的信息
        System.out.println(bean);
    }
}

                运行结果 : 


三、通过指定构造器为Bean注入属性

        1.基本介绍 : 

        “通过指定构造器为Bean注入属性”,即在beans.xml配置文件中通过<constructor-arg/>标签来指定一个JavaBean中的带参构造,利用该带参构造来完成对Bean的属性的初始化,其配置方式本质仍然是“IOC——基于XML文件配置Bean”。如下图所示 : 

        上图中演示的为通过index索引来确定参数,还可以通过type类型或者name属性名来确定参数,本质都是根据形参来唯一确定一个带参构造

        2.应用实例 : 

                需求 : 在beans.xml文件中新配置一个Bean对象,并通过<constructor-arg/>标签指定Student类的一个带参构造来初始化该Bean的属性。在StudentBeanByXML类中新定义一个单元测试方法,通过id属性获取到该Bean对象,并检测属性注入是否成功。
                仍然使用Student类作为JavaBean类,首先我们要在beans.xml文件中配置Bean对象,up为了演示一下index, type, name三种形式的<constructor-arg/>标签,此处配置了三个Bean对象,代码如下 : 

    <bean class="com.cyan.spring.bean.Student" id="stu03">
        <constructor-arg value="Cyan" index="0"></constructor-arg>
        <constructor-arg value="21" index="1"></constructor-arg>
        <constructor-arg value="453" index="2"></constructor-arg>
    </bean>
    <bean class="com.cyan.spring.bean.Student" id="stu04">
        <constructor-arg value="Eisen" type="java.lang.String"></constructor-arg>
        <constructor-arg value="21" type="int"></constructor-arg>
        <constructor-arg value="442" type="int"></constructor-arg>
    </bean>
    <bean class="com.cyan.spring.bean.Student" id="stu05">
        <constructor-arg value="Five" name="name"></constructor-arg>
        <constructor-arg value="20" name="age"></constructor-arg>
        <constructor-arg value="460" name="score"></constructor-arg>
    </bean>

                接着,在StudentBeanByXML类中新定义一个方法,分别获取到id = stu03, id = stu04, id = stu05的对象,insertPropertiesByConstructor()方法代码如下 : 

    //2.通过指定构造器为Bean注入属性
        //回顾————
        //对Bean的管理包括两方面 (1) 创建bean对象;(2) 为bean对象注入属性。
    @Test
    public void insertPropertiesByConstructor() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        Student stu03 = ioc.getBean("stu03", Student.class);
        Student stu04 = ioc.getBean("stu04", Student.class);
        Student stu05 = ioc.getBean("stu05", Student.class);

        System.out.println("stu03 = " + stu03);
        System.out.println("stu04 = " + stu04);
        System.out.println("stu05 = " + stu05);
    }

                运行结果 : 

                可以看到,打印出的属性值与我们配置的一致,说明带参构造成功初始化该bean对象。


四、通过p命名空间为Bean注入属性

        1.基本介绍 : 

        前文中用<bean></bean>标签配置Bean对象时,我们用到了class属性(JavaBean的全类名),id属性(对象在容器中的标识)。现在我们可以用"p:property_name = property_value"(注意冒号)的形式,直接在bean标签内部为Bean注入属性,但直接使用会报错,如下图所示 : 

        报错提示 : "Namespace 'p' is not bound"(命名空间p未绑定)。 

        解决方法 : 将鼠标悬停在报错处,按下Alt + Enter,选择"Create namespace declaration"(创建命名空间声明),如下图所示 :

        创建命名空间声明后, 可以看到beans.xml根元素中已经自动加入了p命名空间的声明,如下图所示 : 

        2.应用实例 : 

                需求 : 在beans.xml文件中新配置一个Bean对象,并通过p命名空间为该对象注入属性。在StudentBeanByXML类中新定义一个单元测试方法,打印该对象信息,检测属性注入是否成功。
                仍然使用Student类作为JavaBean类,首先我们要在beans.xml文件中配置Bean对象,代码如下 : 

    <bean class="com.cyan.spring.bean.Student" id="stu06" p:name="Peter" p:age="33" p:score="1024">

    </bean>

                接着,在StudentBeanByXML测试类中新定义一个方法,获取到id = stu06的对象,injectPropertiesByP()方法代码如下 : 

    //3.通过p命名空间为Bean注入属性
    @Test
    public void injectPropertiesByP() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        Student stu06 = ioc.getBean("stu06", Student.class);

        System.out.println("stu06 = " + stu06);
    }

                运行结果 : 


五、通过ref引用实现Bean的相互引用

        1.基本介绍 : 

        (1) 在JavaWeb中(比方说我们之前的Web项目——尘歌壶摆设购买系统),我们利用“分层设计”的思想,会分别建立DAO层,Service层,Web层;并根据“Web层调用Service层,Service层调用DAO层”的思想,在XxxServlet类中new一个XxxService对象,在XxxService类中new一个XxxDao对象。如下图所示 : 

        (2) 而有了Spring的加持后,我们可以通过ref(引用)来实现IOC容器中bean对象的相互引用,其本质是通过ref引用来为一个对象的引用类型的属性进行初始化,即属于“Bean管理——为bean注入属性”的范畴

        (3) ref在使用时,是作为property标签的一个属性(我们之前已经多次用到了property标签),格式如下 : 

        <property name="property_name" ref="otherBeans id"></property>(更多说明见beans.xml中的注释)

        2.应用实例 : 

                需求 : 参照之前JavaWeb中“分层设计”的思想,在Service层中调用DAO层,但是不直接new出实例,而是在beans.xml文件中通过ref进行配置,试着根据输出语句,测试注入属性是否成功。
                首先,我们需要创建用于测试的Service层和DAO层的类,以我们之前的尘歌壶摆设购买系统为参考,如下图所示 : 

                FurnishingDAOImpl类代码如下 : (定义了一个用于添加摆设的addFurnishing方法)

package com.cyan.spring.dao;

/**
 * @author : Cyan_RA9
 * @version : 21.0
 */
public class FurnishingDAOImpl {
    public FurnishingDAOImpl() {
        System.out.println("FurnishingDAOImpl的无参构造被调用~");
    }
    public void addFurnishing() {
        System.out.println("FurnishingDAOImpl的addFurnishing方法被调用~");
    }
}

                FurnishingServiceImpl类代码如下 : (定义了一个FurnishingDAOImpl类型的属性,我们将在beans.xml文件中对其进行初始化;此外,定义addFurnishing()方法,实现Service层调用DAO层)

package com.cyan.spring.service;

import com.cyan.spring.dao.FurnishingDAOImpl;

/**
 * @author : Cyan_RA9
 * @version : 21.0
 */
public class FurnishingServiceImpl {
    //Service层调用DAO层(但通过beans.xml文件进行配置)
    private FurnishingDAOImpl furnishingDAO;

    public FurnishingServiceImpl() {
    }

    //About DAO's setter,getter
    public FurnishingDAOImpl getFurnishingDAO() {
        return furnishingDAO;
    }

    public void setFurnishingDAO(FurnishingDAOImpl furnishingDAO) {
        this.furnishingDAO = furnishingDAO;
    }

    //Add Furnishing
    public void addFurnishing() {
        System.out.println("FurnishingServiceImpl的addFurnishing()方法被调用~");
        furnishingDAO.addFurnishing();
    }
}

                在beans.xml中配置这两个对象,代码如下 : (注意看注释

    <!--
        (1) 配置一个FurnishingDAOImpl对象;
        (2) 由于该类暂时没有设置属性,所以此处不需要注入属性(下面furnishingService01同理)
     -->
    <bean class="com.cyan.spring.dao.FurnishingDAOImpl" id="furnishingDAO01"/>
    <!--
        (1) 配置一个FurnishingServiceImpl对象;
        (2) 仍然是通过property子元素来为id = furnishingService01的对象注入属性,
            注意,此处的"ref" 表示————
                id = furnishingService01的对象的furnishingDAO属性,
                引用了上面配置的id = furnishingDAO01的对象
            PS : ref体现了Spring的依赖注入(即一个对象的属性,引用了另一个对象)
     -->
    <bean class="com.cyan.spring.service.FurnishingServiceImpl" id="furnishingService01">
        <property name="furnishingDAO" ref="furnishingDAO01"></property>
    </bean>

                最后,仍是在StudentBeanByXML测试类中新定义一个方法,测试FurnishingServiceImpl的FurnishingDAOImpl属性是否被初始化。
                injectPropertiesByRef()方法代码如下 : 

    //4.通过ref引用实现Bean的相互引用
    @Test
    public void injectPropertiesByRef() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        FurnishingServiceImpl furnishingService01 = ioc.getBean("furnishingService01", FurnishingServiceImpl.class);

        furnishingService01.addFurnishing();
    }

                运行结果 : 

                可以看到,FurnishingDAOImpl类的无参构造成功被调用,说明它被成功初始化。
                其实,除了使用ref属性外,还可以直接通过配置内部bean来完成对属性的初始化,如下所示 : 

    <bean class="com.cyan.spring.service.FurnishingServiceImpl" id="furnishingService02">
        <property name="furnishingDAO">
            <bean class="com.cyan.spring.dao.FurnishingDAOImpl"/>
        </property>
    </bean>

                这种方法的意思是,对引用类型属性的初始化不再使用Spring容器中已经配置好的对象,而是自己重新配置一个bean。经过测试,配置内部bean的方法也可以成功注入,大家有兴趣可以自己去试试。


六、对Bean注入属性的内容延伸

        1.准备工作 : 

                上文中我们已经演示过多种为bean注入属性的方式,比如“通过指定构造器注入”,“通过p命名空间注入”,“通过ref引用实现Bean的相互引用”等。现在,让我们来讨论一下,如果bean对象的属性是数组或者集合类型,我们又该怎样去注入呢?

                为了实现“注入数组 or 集合类型的属性”,我们先来创建一个类去维护这些属性,School类代码如下 : 

package com.cyan.spring.bean;

import java.util.*;

/**
 * @author : Cyan_RA9
 * @version : 21.0
 */
public class School {
    private List<Student> studentList;
    private Set<Student> studentSet;
    private Map<String, Student> studentMap;
    private String[] studentNames;
    private Properties pros;

    public School() {

    }
    public School(List<Student> studentList, Set<Student> studentSet, Map<String, Student> studentMap, String[] studentNames, Properties pros) {
        this.studentList = studentList;
        this.studentSet = studentSet;
        this.studentMap = studentMap;
        this.studentNames = studentNames;
        this.pros = pros;
    }

    public List<Student> getStudentList() {
        return studentList;
    }
    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    public Set<Student> getStudentSet() {
        return studentSet;
    }
    public void setStudentSet(Set<Student> studentSet) {
        this.studentSet = studentSet;
    }

    public Map<String, Student> getStudentMap() {
        return studentMap;
    }
    public void setStudentMap(Map<String, Student> studentMap) {
        this.studentMap = studentMap;
    }

    public String[] getStudentNames() {
        return studentNames;
    }
    public void setStudentNames(String[] studentNames) {
        this.studentNames = studentNames;
    }

    public Properties getPros() {
        return pros;
    }
    public void setPros(Properties pros) {
        this.pros = pros;
    }

    @Override
    public String toString() {
        return "School{" +
                "studentList=" + studentList +
                ", studentSet=" + studentSet +
                ", studentMap=" + studentMap +
                ", studentNames=" + Arrays.toString(studentNames) +
                ", pros=" + pros +
                '}';
    }
}

                接着,我们在beans.xml中配置一个School类型的Bean对象,代码如下 : 

    <bean class="com.cyan.spring.bean.School" id="school01">
        <!-- 暂未给属性赋值 -->
    </bean>

        2.注入List类型的属性 : 

                在刚刚配置的id = school01的bean对象中,通过property标签为List类型的属性初始化,代码如下 : 

        <property name="studentList">
            <list>
                <ref bean="stu03"></ref>
                <ref bean="stu04"></ref>
            </list>
        </property>

                注入,除了"通过ref元素配置"的形式外,也可以在List元素中直接配置内部Bean。仍然是在StudentBeanByXML测试类中,我们新定义一个方法测试List属性是否注入成功,injectList()方法代码如下 : 

    //5.为Bean注入集合 or 数组类型的属性
        //5.1 注入List类型的属性
    @Test
    public void injectList() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        School school01 = ioc.getBean("school01", School.class);

        System.out.println("School中的studentList属性如下 : ");
        System.out.println(school01.getStudentList());
    }

                运行结果 : 

        3.注入Set类型的属性 : 

                在“准备工作”中配置的id = school01的bean对象中,通过property标签为Set类型的属性初始化,代码如下 : 

        <property name="studentSet">
            <set>
                <ref bean="stu03"></ref>
                <ref bean="stu06"></ref>
            </set>
        </property>

                可以看到,Set类型属性的配置和List类型属性的配置非常类似,只不过前者是放在set元素里了。仍然是在StudentBeanByXML测试类中,我们新定义一个方法测试Set属性是否注入成功,injectSet()方法代码如下 : 

        //5.2 注入Set类型的属性
    @Test
    public void injectSet() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        School school01 = ioc.getBean("school01", School.class);

        System.out.println("School中的studentSet属性如下 : ");
        System.out.println(school01.getStudentSet());
    }

                运行结果 : 

        4.注入Map类型的属性 : 

                在“准备工作”中配置的id = school01的bean对象中,通过property标签为Map类型的属性初始化,代码如下 : 

        <property name="studentMap">
            <map>
                <!-- entry表示一个键值对  -->
                <entry>
                    <!-- key元素用于配置key,注意此处的value是指key的值,字面意思 -->
                    <key>
                        <value>keyStu05</value>
                    </key>
                    <!-- 紧跟key元素其后的ref元素才是真正的当前键值对的value! -->
                    <ref bean="stu05"></ref>
                </entry>
                <entry>
                    <key>
                        <value>keyStu06</value>
                    </key>
                    <ref bean="stu06"></ref>
                </entry>
            </map>
        </property>

                仍然是在StudentBeanByXML测试类中,我们新定义一个方法测试Map属性是否注入成功,injectMap()方法代码如下 : 

        //5.3 注入Map类型的属性
    @Test
    public void injectMap() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        School school01 = ioc.getBean("school01", School.class);

        System.out.println("School中的studentMap属性如下 : ");
        System.out.println(school01.getStudentMap());
    }

                运行结果 : 

        5.注入数组类型的属性 : 

                在“准备工作”中配置的id = school01的bean对象中,通过property标签为String[]类型的属性初始化,代码如下 : 

        <property name="studentNames">
            <array>
            <!-- 由于School类中维护的数组为String类型,所以此处直接以value元素配置 -->
                <value>Cyan</value>
                <value>Rain</value>
                <value>Five</value>
                <value>Ice</value>
            </array>
        </property>

                仍然是在StudentBeanByXML测试类中,我们新定义一个方法测试String[]属性是否注入成功,injectArray()方法代码如下 : 

        //5.4 注入数组类型的属性
    @Test
    public void injectArray() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        School school01 = ioc.getBean("school01", School.class);

        System.out.println("School中的studentNames属性如下 : ");
        System.out.println(Arrays.toString(school01.getStudentNames()));
    }

                运行结果 : 

        6.注入Properties类型的属性 : 

                在“准备工作”中配置的id = school01的bean对象中,通过property标签为Properties类型的属性初始化,代码如下 : 

        <property name="pros">
            <props>
                <prop key="username">Cyan_RA9</prop>
                <prop key="password">55555233</prop>
                <prop key="ip">127.0.0.1</prop>
            </props>
        </property>

                仍然是在StudentBeanByXML测试类中,我们新定义一个方法测试Properties属性是否注入成功,injectProperties()方法代码如下 : 

        //5.5 注入Properties类型的属性
    @Test
    public void injectProperties() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        School school01 = ioc.getBean("school01", School.class);

        System.out.println("School中的pros属性如下 : ");
        System.out.println(school01.getPros());
    }

                运行结果 : 

        7.List属性注入之通过util命名空间注入 : 

                方才我们已经见过了如何通过property元素注入List类型的属性,其实是通过list子元素来实现的。
                那么,假设现在给出一个需求:已知成华大道到二仙桥的路上开着两家书店,它们都卖《生死疲劳》《镜花缘》《湘行散记》《明朝那些事儿》《三体》这几本书,让你在beans.xml中配置这俩个书店对象,你能吗?
                你可能会说:哟瞧你这话说的,这不是张飞吃豆芽——小菜一碟么?看我一波张飞穿针——粗中有细,给你整得明明白白,服服帖帖。

                于是你就开整了,先新定义一个BookStore的JavaBean类,代码如下 : 

package com.cyan.spring.bean;

import java.util.List;

public class BookStore {
    private List<String> bookList;

    public BookStore() {
    }
    public BookStore(List<String> bookList) {
        this.bookList = bookList;
    }

    public List<String> getBookList() {
        return bookList;
    }
    public void setBookList(List<String> bookList) {
        this.bookList = bookList;
    }
}

                再去beans.xml文件中配置一波,代码如下 : 

    <bean class="com.cyan.spring.bean.BookStore" id="bookStore01">
        <property name="bookList">
            <list>
                <value>生死疲劳</value>
                <value>镜花缘</value>
                <value>湘行散记</value>
                <value>明朝那些事儿</value>
                <value>三体</value>
            </list>
        </property>
    </bean>
    <bean class="com.cyan.spring.bean.BookStore" id="bookStore02">
        <property name="bookList">
            <list>
                <value>生死疲劳</value>
                <value>镜花缘</value>
                <value>湘行散记</value>
                <value>明朝那些事儿</value>
                <value>三体</value>
            </list>
        </property>
    </bean>

                你还不尽兴, 继续去测试类中定义了一个单元测试的方法,设法输出bookList属性进行检验,testMySB()方法代码如下 : 

    @Test
    public void testMySB() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        BookStore bookStore01 = ioc.getBean("bookStore01", BookStore.class);
        BookStore bookStore02 = ioc.getBean("bookStore02", BookStore.class);

        System.out.println("bookStore01's bookList = " + bookStore01.getBookList());
        System.out.println("bookStore02's bookList = " + bookStore02.getBookList());
    }

                运行结果 : 

                对此,我只能说:“你™是真🐂B呀”。是的,坦白说你做的针不戳儿。但是呢,假如现在成华大道到二仙桥的路上又开了5家书店,阁下又如何应对呢?
                你可能会想:那我再配5个Bean不就完事儿了么,这up🐖怎么磨磨唧唧的,阴阳怪气,你到底想说啥你说呗,整这么绕一大圈子。对此,我想说:“你再配5个Bean也确实能成事儿,但我说你这么配就慢了,我们不仅要配得对,而且要配得快。

                于是便要引出util命名空间了。直接上代码 : (当你使用<util:list>时,IDEA会自动帮你引入util命名空间)

    <util:list id="commonBookList">
        <value>生死疲劳</value>
        <value>镜花缘</value>
        <value>湘行散记</value>
        <value>明朝那些事儿</value>
        <value>三体</value>
    </util:list>

    <bean class="com.cyan.spring.bean.BookStore" id="bookStore01">
        <property name="bookList" ref="commonBookList">
        </property>
    </bean>
    <bean class="com.cyan.spring.bean.BookStore" id="bookStore02">
        <property name="bookList" ref="commonBookList">
        </property>
    </bean>

                可以看到,其实就是将大家都有的📕放到了<util:list></util:list>元素中,然后在每个BookStore类型的Bean对象中,利用ref引用到<util:list>中。经测试,输出结果是一样的,这里就不再放图了。

        8.级联属性注入 : 

                所谓“级联属性注入”,其实指的是Spring的IOC容器可以直接为“属性的属性”赋值,即当类中的某个属性有自己的属性时,我们希望在配置该类Bean对象时将对象属性和对象属性的属性都注入。

                需求 : 定义一个员工类,维护员工id,员工姓名,员工的部门名称三个属性,其中,员工的部门名称通过部门类的deptName属性表示。要求设法实现“级联属性注入”。
                首先,我们需要定义员工类和部门类,如下 : 
                Employee类代码如下 : 

package com.cyan.spring.bean;

public class Employee {
    private Integer id;
    private String name;
    private Department department;

    public Employee() {
    }
    public Employee(Integer id, String name, Department department) {
        this.id = id;
        this.name = name;
        this.department = department;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

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

    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", department=" + department +
                '}';
    }
}

                Department类代码如下: 

package com.cyan.spring.bean;

public class Department {
    private String deptName;

    public Department() {
    }
    public Department(String deptName) {
        this.deptName = deptName;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    @Override
    public String toString() {
        return "Department{" +
                "deptName='" + deptName + '\'' +
                '}';
    }
}

                接着,在beans.xml文件中配置Employee对象和Department对象。代码如下 : 

    <!-- 配置Employee类对象 -->
    <bean class="com.cyan.spring.bean.Employee" id="employee01">
        <property name="id" value="1"></property>
        <property name="name" value="Cyan"></property>
        <property name="department" ref="department01"></property>
        <!-- [级联属性注入] -->
        <!-- 其实是通过"对象名.属性名"的形式,对属性的属性进行注入操作 -->
        <property name="department.deptName" value="Back-End"></property>
        <!-- 此处,底层实际调用了setDeptName方法 -->
    </bean>
    <!-- 配置Department类对象 -->
    <bean class="com.cyan.spring.bean.Department" id="department01"/>

                最后,在测试类StudentBeanByXML中新定义一个单元测试方法,输出配置的Employee对象,查看级联属性注入是否成功。testCascade()方法代码如下 : 

    //PS : 测试级联属性赋值
    @Test
    public void testCascade() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        Employee employee01 = ioc.getBean("employee01", Employee.class);

        System.out.println("employee01 = \n" + employee01);
    }

                运行结果 : 

                可以看到,employee对象的属性department的属性deptName被成功初始化,说明级联属性注入成功。


七、通过静态工厂获取Bean

        1.基本介绍 : 

        “通过静态工厂获取Bean”,本质上还是“基于XML方式配置Bean”;只不过我们并不直接在配置Bean时就为Bean对象注入属性,而是事先在静态工厂类的static代码块中完成了初始化(用静态的Map容器来存储Student类对象),并提供了一个静态方法用于获取已经初始化好的Student对象,然后在beans.xml配置文件中,我们只需要给定一个key,并指定调用静态工厂提供的用于获取Student对象的方法,Spring容器便可以根据该key获取到对应的Student类对象。

        注意 : 

        (1) “通过静态工厂获取Bean”,在配置bean时,class不再是Student类的全路径,而是静态工厂类的全路径.

        (2) 除了id和class外,还需要一个属性factory-method,表示指定一个静态工厂类的用于返回Bean对象的方法.

        (3) 至于bean元素内部,则需要使用<construcotr-arg value="key"/>标签,说明要获取的对象在静态工厂类中对应的key

        2.应用实例 : 

                上面说了一堆,只是看肯定多少觉得一头雾水,下面我们来个实例感受一下。
                首先,up定义一个自己的静态工厂类,CyanStaticFactory类代码如下 : 

package com.cyan.spring.factory;

import com.cyan.spring.bean.Student;

import java.util.HashMap;
import java.util.Map;

public class CyanStaticFactory {
    //维护一个静态Map集合,用于保存Student对象
    private static Map<String, Student> studentMap;

    //在静态代码块中初始化Student对象
    static {
        studentMap = new HashMap<>();

        studentMap.put("student01", new Student("Cyan", 21, 450));
        studentMap.put("student02", new Student("Rain", 19, 460));
    }

    //提供一个获取Student对象的静态方法
    public static Student getStudent(String key) {
        return studentMap.get(key);
    }
}

                然后在beans.xml文件中完成配置,代码如下 : (注意看up配置的id,表明最终返回的其实是一个Student类对象)

    <bean class="com.cyan.spring.factory.CyanStaticFactory" id="stu07"
        factory-method="getStudent">
        <constructor-arg value="student02"/>
    </bean>

                最后,仍然是在测试类StudentBeanByXML中,定义一个单元测试方法,获取到Student对象,
                getBeanByStaticFactory()方法代码如下 : (注意此处getBean方法得到的是Student类型的对象)

    //6.通过静态工厂获取Bean
    @Test
    public void getBeanByStaticFactory() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        Student stu07 = ioc.getBean("stu07", Student.class);

        System.out.println(stu07);
    }

                运行结果 : 


八、通过实例工厂获取Bean

        1.基本介绍 : 

        “通过实例工厂获取Bean”,和通过静态工厂获取Bean类似,只不过见名知意,我们在自定义实例工厂类中通过非静态Map容器来保存Student类对象,并在非静态代码块中对Map容器进行初始化,并提供一个用于获取Student对象的非静态方法。然后在beans.xml文件中,除了指定一个用于获取Bean对象的方法,以及给出key外,必须先指定一个实例工厂对象

        回顾一下代码块——

        静态代码块随着类的加载而被隐式地调用,最多只能执行一次而对于非静态代码块,每实例化一次包含该非静态代码块的类,都会执行一次该类中的非静态代码块

        结合代码块的内容回顾,我们可以猜到必须先实例化“实例工厂对象”,以执行其非静态代码块中的内容,完成对非静态Map集合的初始化;然后才能获取到其保存的学生对象

        注意 : 

        (1) “通过实例工厂获取Bean”,在配置bean时,需要同时配置实例工厂对象和学生对象.

        (2) 属性factory-method,表示指定一个实例工厂类的用于返回Bean对象的方法;属性factory-bean,表示指定使用一个特定的实例工厂对象返回Bean

        (3) bean元素内部,仍需要使用<construcotr-arg value="key"/>标签,说明要获取的对象在实例工厂类中对应的key

        2.应用实例 : 

                首先,up定义一个自己的实例工厂类,CyanInstanceFactory类代码如下 : 

package com.cyan.spring.factory;

import com.cyan.spring.bean.Student;

import java.util.HashMap;
import java.util.Map;

public class CyanInstanceFactory {
    private Map<String, Student> studentMap;

    {
        studentMap = new HashMap<>();

        studentMap.put("student03", new Student("Eisen", 22, 437));
        studentMap.put("student04", new Student("Five", 20, 429));
    }

    public Student getStudent(String key) {
        return studentMap.get(key);
    }
}

                然后在beans.xml文件中完成配置,代码如下 :

    <!-- 配置实例工厂对象 -->
    <bean class="com.cyan.spring.factory.CyanInstanceFactory" id="cyanInstanceFactory01"/>
    <!-- 配置学生对象 -->
    <bean id="stu08" factory-bean="cyanInstanceFactory01" factory-method="getStudent">
        <constructor-arg value="student03"/>
    </bean>

                最后,仍然是在测试类StudentBeanByXML中,定义一个单元测试方法,获取到Student对象,
                getBeanByInstanceFactory()方法代码如下 :

    //7.通过实例工厂获取Bean
    @Test
    public void getBeanByInstanceFactory() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        Student stu08 = ioc.getBean("stu08", Student.class);

        System.out.println(stu08);
    }

                运行结果 : 


九、通过FactoryBean获取Bean

        1.基本介绍 : 

        “通过FactoryBean获取Bean”,又和上文中通过实例工厂获取Bean类似,都是维护了一个非静态Map容器来保存Bean对象,并在非静态代码块中对Map容器进行初始化,此外,还需要单独维护一个String类型的key属性。并且,我们需要去实现FactoryBean<>接口,并重写接口中的方法

        注意 : 

        (1) “通过FactoryBean获取Bean”,在配置bean时,class为FactoryBean的全类名.

        (2) 通过property子元素为key属性初始化

        2.应用实例 : 

                首先,我们需要定义一个自己的FactoryBean类并实现FactoryBean接口,CyanFactoryBean类代码如下 : 

package com.cyan.spring.factory;

import com.cyan.spring.bean.Student;
import org.springframework.beans.factory.FactoryBean;

import java.util.HashMap;
import java.util.Map;

public class CyanFactoryBean implements FactoryBean<Student> {
    private String key;
    private Map<String, Student> studentMap;
    {
        studentMap = new HashMap<>();

        studentMap.put("student05", new Student("Irving", 32, 427));
        studentMap.put("student06", new Student("Rose", 23, 431));
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public Student getObject() throws Exception {
        return studentMap.get(key);
    }

    @Override
    public Class<?> getObjectType() {
        return Student.class;
    }

    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}

                接着,在beans.xml中配置bean对象,代码如下 : 

    <!-- 配置Student对象,通过FactoryBean获取 -->
    <bean class="com.cyan.spring.factory.CyanFactoryBean" id="stu09">
        <property name="key" value="student05"/>
    </bean>

                最后,在StudentBeanByXML测试类中定义一个单元测试方法,测试是否配置成功。getBeanByFactoryBean()方法代码如下 : 

    //8.通过实例工厂获取Bean
    @Test
    public void getBeanByFactoryBean() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

        Student stu09 = ioc.getBean("stu09", Student.class);

        System.out.println(stu09);
    }

                运行结果 : 


十、关于Bean配置的更多内容和细节

        由于“Spring IOC—基于XML配置和管理Bean”内容较多,而up写到这里时编辑器已经很卡了😂。故打算将Bean配置信息重用,Bean生命周期,以及Bean后置处理器等内容单独放一篇文章中。

        链接如下 : 

        待更新---🕊🕊


十一、总结

  •  🆗,以上就是Spring系列博文第二小节的全部内容了。
  • 总的来看,Spring 基于XML配置和管理Bean内容很多,我们可以通过多种方式获取Bean或者为Bean注入属性,足以感受到Spring配置和管理Bean的灵活性。再来简单回顾一下上文的总述,如下图所示 :

  • 下一节内容——Spring IOC——基于注解配置和管理Bean感谢阅读!

        System.out.println("END-------------------------------------------------"); 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/213872.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

手机也能“敲”代码?

除了PC个人电脑外&#xff0c;很多电子产品也可以实现代码的编辑&#xff0c;比如智能手机。现在主流的手机操作系统只有两种&#xff0c;一种是大部分手机厂商选择的安卓系统&#xff0c;另外一种是苹果公司独创的ios操作系统。而Android系统是基于Linux开发的专属于移动设备的…

【Leetcode题单】(01 数组篇)刷题关键点总结03【数组的改变、移动】

【Leetcode题单】&#xff08;01 数组篇&#xff09;刷题关键点总结03【数组的改变、移动】&#xff08;3题&#xff09; 数组的改变、移动453. 最小操作次数使数组元素相等 Medium665. 非递减数列 Medium283. 移动零 Easy 大家好&#xff0c;这里是新开的LeetCode刷题系列&…

Java数据结构之《构造哈夫曼树》题目

一、前言&#xff1a; 这是怀化学院的&#xff1a;Java数据结构中的一道难度中等(偏难理解)的一道编程题(此方法为博主自己研究&#xff0c;问题基本解决&#xff0c;若有bug欢迎下方评论提出意见&#xff0c;我会第一时间改进代码&#xff0c;谢谢&#xff01;) 后面其他编程题…

【蓝桥杯】翻硬币

翻硬币 思路&#xff1a; 其实有点贪心的意思&#xff0c;依次比较&#xff0c;不同就1&#xff0c;然后修改自己的字符串和下一个的字符串&#xff0c;再匹配。 #include<iostream> #include<string> using namespace std;string now,res;int main(void) {cin&g…

MQ - 消息系统

消息系统 1、消息系统的演变 在大型系统中&#xff0c;会需要和很多子系统做交互&#xff0c;也需要消息传递&#xff0c;在诸如此类系统中&#xff0c;你会找到源系统&#xff08;消息发送方&#xff09;和 目的系统&#xff08;消息接收方&#xff09;。为了在这样的消息系…

java高校实验室排课学生考勤系统springboot+vue

随着各高校办学规模的迅速扩大,学科专业的不断拓宽,传统的实验教学和实验室管理方法已经不能适应学校管理的要求,特别是化学实验室的管理,化学实验室仪器药品繁杂多样,管理任务繁重,目前主要使用人工记录方法管理,使用不便,效率低下,而且容易疏漏.时间一长将产生大量的文件和数…

【面试经典150 | 二分查找】搜索二维矩阵

文章目录 写在前面Tag题目来源题目解读解题思路方法一&#xff1a;二分查找 写在最后 写在前面 本专栏专注于分析与讲解【面试经典150】算法&#xff0c;两到三天更新一篇文章&#xff0c;欢迎催更…… 专栏内容以分析题目为主&#xff0c;并附带一些对于本题涉及到的数据结构等…

c# OpenCV 读取、显示和写入图像(二)

读取、显示和写入图像是图像处理和计算机视觉的基础。即使在裁剪、调整大小、旋转或应用不同的滤镜来处理图像时&#xff0c;您也需要先读取图像。因此&#xff0c;掌握这些基本操作非常重要。 imread()读取图像imshow()在窗口中显示图像imwrite()将图像保存到文件目录里 我们…

细说CountDownLatch

CountDownLatch 概念 CountDownLatch可以使一个获多个线程等待其他线程各自执行完毕后再执行。 CountDownLatch 定义了一个计数器&#xff0c;和一个阻塞队列&#xff0c; 当计数器的值递减为0之前&#xff0c;阻塞队列里面的线程处于挂起状态&#xff0c;当计数器递减到0时…

力扣226:翻转二叉树

力扣226&#xff1a;翻转二叉树 给你一棵二叉树的根节点 root &#xff0c;翻转这棵二叉树&#xff0c;并返回其根节点。 示例 1&#xff1a; 输入&#xff1a;root [4,2,7,1,3,6,9] 输出&#xff1a;[4,7,2,9,6,3,1] 示例 2&#xff1a; 输入&#xff1a;root [2,1,3]…

分享一个国内可用的免费AI-GPT网站

背景 ChatGPT作为一种基于人工智能技术的自然语言处理工具&#xff0c;近期的热度直接沸腾&#x1f30b;。 我们也忍不住做了一个基于ChatGPT的网站&#xff0c;可以免登陆&#xff01;&#xff01;国内可直接对话AI&#xff0c;也有各种提供工作效率的工具供大家使用。 可以这…

判断二叉树是否为完全二叉树

具体思路&#xff1a; 将二叉树层序遍历&#xff08;节点&#xff09;插进队列中&#xff0c;遇到空时就break&#xff08;退出循环&#xff09;&#xff0c;再重新遍历一遍&#xff0c;若空的后面又再次出现数据&#xff0c;则返回false&#xff08;不是完全二叉树&#xff0…

SpringSecurity和JWT实现认证和授权

SpringSecurity和JWT实现认证和授权 框架介绍SpringSecurityJWT组成实例JWT实现认证和授权的原理 Hutool 使用表整合SpringSecurity及JWT在pom.xml中添加依赖添加JWT token的工具类添加RbacAdminService&#xff1a;添加自定义mapper创建SpringSecurity配置类添加ProjectSecuri…

软件工程精品课程教学网站的设计与实现

系统功能需求分析 本系统要求采用Browser/Server模式设计开发&#xff0c;可以作为一般高等院校的网络学堂&#xff1b;可以为教师的辅助教学或者网络教学提供一个完善的教学网站&#xff1b;学生可以利用本教学网站来完成一些课程的学习任务。 2.2.1 功能划分 《软件工程》教学…

分享一个简单的基于C语言嵌入式GUI界面切换代码

目录 前言 一、数据类型 二、页面调度 三、页面显示 四、视频展示 前言 最近在用LVGL写一个简单的UI界面&#xff0c;需要进行几个页面的切换&#xff0c;所以就自己写了一个简单页面切换代码&#xff0c;方便进行页面切换&#xff0c;同时使UI代码结构更加清晰。这个结构…

如何使用注解实现接口的幂等性校验

如何使用注解实现接口的幂等性校验 背景什么是幂等性为什么要实现幂等性校验如何实现接口的幂等性校验1. 数据库唯一主键2. 数据库乐观锁3. 防重 Token 令牌4. redis 如何将这几种方式都组装到一起结语 背景 最近在小组同学卷的受不了的情况下&#xff0c;我决定换一个方向卷去…

人工智能轨道交通行业周刊-第67期(2023.11.27-12.3)

本期关键词&#xff1a;列车巡检机器人、城轨智慧管控、制动梁、断路器、AICC大会、Qwen-72B 1 整理涉及公众号名单 1.1 行业类 RT轨道交通人民铁道世界轨道交通资讯网铁路信号技术交流北京铁路轨道交通网上榜铁路视点ITS World轨道交通联盟VSTR铁路与城市轨道交通RailMetro…

使用Prometheus监控Padavan路由器

Prometheus监控Padavan路由器 1、背景 近期在Synology&#xff08;群辉&#xff09;中安装一套Prometheus监控程序&#xff0c;目前已经监控Synology&#xff0c;然后家中有有路由器&#xff08;Padavan&#xff09;型号&#xff0c;也准备使用PrometheusGrafan进行监控。 ‍…

Java基本数据类型详解

✨个人主页&#xff1a;全栈程序猿的CSDN博客 &#x1f4a8;系列专栏&#xff1a;Java从入门到精通 ✌座右铭&#xff1a;编码如诗&#xff0c;Bug似流星&#xff0c;持续追求优雅的代码&#xff0c;解决问题如同星辰般自如 Java是一种强类型语言&#xff0c;数据类型在程序中起…

如何成为一名高效的前端开发者(10X开发者)

如今&#xff0c;每个人都想成为我们所说的“10倍开发者”。然而&#xff0c;这个术语经常被误解和高估。 本质上&#xff0c;一个高效或者10倍开发者&#xff0c;在我看来&#xff0c;是指那些能够充分利用所有可用工具的人&#xff0c;通过让这些工具处理冗余和重复的任务&am…
最新文章