spring源码环境搭建
1.环境准备
1.下载配置jdk11。
2.配置gradle的仓库地址:
windows环境添加环境变量:
变量名:GRADLE_USER_HOME
value:你存放下载包的文件夹

2.下载源码并编译
下载编译前最好要FQ,不然会很慢。
我使用的是ssrFQ。下载编译是在git shell上操作的。
所以下载编译前git需要设置代理:
git config --global https.proxy http://127.0.0.1:1080 git config --global https.proxy https://127.0.0.1:1080 git config --global http.proxy socks5h://127.0.0.1:1080
取消代理命令:
git config --global --unset http.proxy git config --global --unset https.proxy
官方源码下载编译文档:(写的很清楚明了)
https://github.com/spring-projects/spring-framework/wiki/Build-from-Source
我照着文档的操作
下载:
git clone git@github.com:spring-projects/spring-framework.git
cd spring-framework
编译:
./gradlew build
导入idea:
1.导入前需要预编译:
./gradlew :spring-oxm:compileTestJava
2.导入前设置gradle仓库地址、idea的http代理(加快下载外网资源速度。没有FQ,可以配置build.gradle,使用阿里镜像下载):



3.导入代码:

等一会就好了

3.测试
0.切换分支:
点击idea右下角git:master,切换成5.2.x。

1.新增springdemo模块:


2.build.gradle增加依赖
compile(project(":spring-context"))

3.写代码测试
如图创建包和类:

WelcomeService类:
public interface WelcomeService { String sayHello(String name); }
WelcomeServiceImpl类:
@Service public class WelcomeServiceImpl implements WelcomeService { @Override public String sayHello(String name) { System.out.println("欢迎你:" + name); return "success"; } }
WelcomeController类:
@Controller public class WelcomeController {private ApplicationContext myContainer; @Autowired private WelcomeService welcomeService; public void handleRequest(){ welcomeService.sayHello("来自Controller的问候"); } }
Entrance类:
@Configuration @ComponentScan("com.imooc") public class Entrance { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Entrance.class); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for(String beanDefinitionName : beanDefinitionNames){ System.out.println(beanDefinitionName); } WelcomeController welcomeController = (WelcomeController) applicationContext.getBean("welcomeController"); welcomeController.handleRequest(); } }
运行Entrance类的main方法,输出:
欢迎你:来自Controller的问候
表明正常,至此可以打断点深入了解spring源码了。

浙公网安备 33010602011771号