欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

跟着步骤读懂Spring源码并添加个人注解:轻松上手的初级指南

最编程 2024-02-22 16:19:05
...

1、找到spring源码的github地址

github地址 https://github.com/spring-projects/spring-framework

image.png

2、选择自己需要的版本
image.png

3、复制github代码拉取路径
image.png

4、idea拉取代码

因为比较懒 不想先拉下来再导入idea,我这里直接用idea拉取


image.png

image.png

5、拉取完后进行编译

这里有个import-into-idea.md文件,里面也有步骤


image.png
  • 编译前最好改一些依赖下载的仓库,不然下载依赖速度感人
    修改文件:build.gradle
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
image.png

image.png

改完可以编译构建了,编译命令

gradlew :spring-oxm:compileTestJava
image.png

编译的时候会下载一些依赖包 一般很慢,编译完右边会出来gradle项目依赖


image.png

6、编译好了,随便点一个测试类试一下是成功的
image.png

7、为了方便区分源码和自己的测试代码,我一般自己新加一个测试模块
image.png
image.png

接下来一路点next,添加模块名称就好了
添加完模块,总项目的setting.gradle文件会把你的模块添加进去,多一行include


image.png

8、重要的事情:需要在自己添加的模块的构建文件中,增加依赖

spring-context和spring-instrument、junit这几个必须的,不然一会会报错,说包找不到
添加完可以去右上角的gradle依赖按钮刷新一下


image.png

image.png

9、开始写测试代码
image.png

这几个文件不截图了 直接上代码,截图太累
类代码

package org.springframework.beans;

/**
 * @author 
 * @descrtption
 * @time 2021/12/25 18:09
 */
public class MyTestBeanCreate {
    private String userName;
    private String passWord;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "MyTestBeanCreate{" +
                "userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                '}';
    }
}

配置文件

<?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="testhello" class="org.springframework.beans.MyTestBeanCreate">

    </bean>
</beans>

测试代码

import org.junit.Test;
import org.springframework.beans.MyTestBeanCreate;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Assert;

/**
 * @author 
 * @descrtption
 * @time 2021/12/25 18:14
 */
public class MyhelloTest {

    @Test
    public void testMyHello(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-mytest.xml");
        MyTestBeanCreate doimyakon =null;
        Assert.hasText("doim yakon","doimyakon must not be null");
        Assert.notNull(doimyakon,"doimyakon must not be null");
        doimyakon = applicationContext.getBean(MyTestBeanCreate.class);

        Assert.notNull(doimyakon.getUserName(),"UserName must not be null");
        System.out.println(doimyakon);
    }
}

新手入坑,如有错误,欢迎评论指教!!!
有不完善的也欢迎评论,我有空会补充哦!
欢迎关注!我会出后续spring源码学习笔记哦!!!