狂撸Spring源码(一),剑指高级架构

耳一文

关注

阅读 18

2022-03-15

一、Spring源码下载编译

  • 学习Spring源码之前,首先我们需要到GITHUB上下载Spring源码:

     

    image.png

  • 打开IDEA编译器构建源码,Spring使用的Gradle构建的:

     

    image.png

     

    构建成功后

     

    image.png

二、万事具备,接下来就要开始我们的Spring源码学习了!!!本人也是第一次阅读Spring源码,如果有错误,请各位包涵并指出

1.了解Spring 的 IOC 容器

1)创建测试类,方便我们对Spring源码进行Debug学习

image.png

  • 新建module

     

    image.png

  • 添加Gradle依赖

plugins {
    id 'java'
}

group 'org.springframework'
version '5.3.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    compile(project(":spring-beans"))
    compile(project(":spring-core"))
    testCompile group: 'junit', name: 'junit', version: '4.10'
}

  • 新建实体类 MyBean

public class MyBean {
    private String name = "我是MyBean";

    public String getName() {
        return name;
    }

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

  • 新建xml文件,用来注册bean

<?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="myBean" class="MyBean"/>

</beans>
  • 新建测试类

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class SpringBeanFactoryStudyTest {
    @Test
    public void testXmLLoadBean(){
        //通过XML配置文件获取BeanFactory实例
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("application-context.xml"));
        //根据注册Bean的名称获取类实例
        MyBean bean = (MyBean) beanFactory.getBean("myBean");
        System.out.println(bean.getName());
    }
}

  • 执行结果



作者:一个头发茂密的程序员
链接:https://www.jianshu.com/p/cab2ff36dbff

相关资源:

GitOps 初探 - Dcsdn

mysql教程 - Dcsdn

Python入门基础教程全套 - Dcsdn

精彩评论(0)

0 0 举报