Spring 作为 Java 语言的核心框架,深入学习的关键点就是阅读源码,但是阅读源码又是很困难的一件事,因为 Spring 的源码很复杂,如果我们不debug 的话是根本阅读不懂得,所以这篇文章记录了我搭建 Spring 源码开发环境的过程,如果照做到最后,应该可以搭建出和我一样的源码开发环境,如有问题,欢迎留言。
IDEA 从 Github 导入项目,为了方便起见建议 idea 更新到 2019 以上版本,旧版本的 gradle 可能存在兼容问题 构建 Spring-Framework 源码需要以下插件,都可以在 IDEA 中的 插件商店安装
1.gradle 插件
2.kotlin 插件
首先创建项目选择从版本管理器获取
填写 github spring-framework 源码地址:git@github.com:spring-projects/spring-framework.git
点击 Create
构建索引,如果是暂停状态记得点击开始
看到以下窗口标识构建成功
选择 JDK (建议 1.8)
又要进行漫长的索引构建
构建完毕
创建一个新的 Gradle 模块,名字随意
添加完成后,重命名 build.gradle
文件为 spring-grade-demo-web.gradle
替换如下内容
plugins {
id 'java'
id 'war'
}
group 'org.springframework'
version '5.2.6.BUILD-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
description = "Spring Gradle Demo"
apply plugin: "kotlin"
dependencies {
compile(project(":spring-webmvc"))
compile(project(":spring-web"))
}
整体目录截图,下面一步一步创建:
创建 SpringMVC 配置文件:spring-mvc-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.demoWeb.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
创建 web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
创建 /WEB-INF/view/index.jsp
<%--
Created by IntelliJ IDEA.
User: wangjie
Date: 2020/3/29
Time: 9:07 上午
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
测试一下
</body>
</html>
创建 Controller :DemoController tips:这里注意,为什么写上了 spring 的注释,因为 checkStyle 插件会去检测你的代码符不符合项目规范,如果不符合就会编译不通过,按照格式这么写就行
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.demoWeb.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 测试Controller.
*
* @author wangjie
* @since 5.0
*/
@Controller
@RequestMapping
public class DemoController {
@RequestMapping("/test")
public String index(){
return "index";
}
}
然后尝试 build 一下 war包
一切 OK 部署 Tomcat 启动
尝试调试 DispatcherServlet