Spring(二)执行流程解析

简单的通过把bean注册到 Spring 容器中,再从容器中获取bean来分析Spring的执行流程分析。

1.创建module

new --> module --> Gradle --> Java --> spring-demo

spring执行过程.png

2. 在build.gradle中引入依赖

dependencies {
    compile(project(":spring-core"))
    compile(project(":spring-aop"))
    compile(project(":spring-beans"))1
    compile(project(":spring-context"))
    compile(project(":spring-context-support"))
    compile(project(":spring-web"))
    compile(project(":spring-webmvc"))
    compile(project(":spring-orm"))
    compile(project(":spring-jdbc"))
    compile(project(":spring-instrument"))
    compile(project(":spring-tx"))
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

3.新建相应的类文件以及配置文件

实体类

public class Student {

    private String name;

    private int age;

    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;
    }
}

在resources文件夹下创建spring配置文件
相对于spring内核来说,主要是通过两种方式来定义bean:

  • xml
  • 注解

以下是通过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">
    <bean id="student" class="com.colors.spring.bean.Student">
        <property name="name" value="colors"/>
        <property name="age" value="18"/> 
        
    </bean>
</beans>

可以看到这里 age 的类型是String,而我们定义的是 int,实际上 spring 在自动注入的时候,会通过对应的类型转换器来实现相应类型的转换

4. 执行类:

import com.colors.spring.bean.Student;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * 1. 需要通过spring抽象出的各种resource来指定相应的配置文件
 * 2. 显示声明一个spring工厂,该工厂用来掌控我们的配置文件中所生命的各种bean以及bean之间的依赖关系和注入关系
 * 3. 需要定义一个配置信息读取器,该读取器用来读取之前所定义的bean配置信息
* 4. 读取器读取 xml 里面的信息,把读取的到信息装配到 defaultListableBeanFactory 里面,工厂再对 bean 进行管理
 */

public class SpringDemo {
    public static void main(String[] args) {

        // 1.指定加载的资源文件
        Resource resource = new ClassPathResource("spring-config.xml");

        // 2.创建管理bean的工厂
        DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();

        // 3.资源读取器,把读取的到信息装配到 defaultListableBeanFactory 里面,工厂再对 bean 进行管理
        BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory);

        // 4.读取 xml 里面的信息
        beanDefinitionReader.loadBeanDefinitions(resource);

        // 5.获取 bean
        Student student = defaultListableBeanFactory.getBean("student", Student.class);

        System.out.println(student.getName());
        System.out.println(student.getAge());

    }
}

执行结果:

springdemo执行结果.png

至此已成功从 defaultListableBeanFactory 工厂中获取到了我们bean

5.总结

日常开发中我们很少会注意到spring是如何加载bean的,因为这些都由spring的核心组件帮我们完成。这个过程中,我们发现了一个很重要的概念:Bean工厂。实际上 spring 通过 Bean工厂 把工厂模式发挥了极致。这个理念在spring成立之初到现在也没有发生变化,可以看出spring的理念非常超前。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容