大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
阿里云服务器优惠券领取 优惠券
聚会小游戏,大家可以体验下。
目标:搭建起Spring源码阅读和代码调试跟踪的环境,顺便建立一个简单的Demo,能够调试Spring的源代码
1. 资源准备
jdk版本: 1.8.0_202
IDEA版本: 2020.2.1
aspectj版本: aspectj-1.9.2
sourcetree版本: 3.3.8
2. 源码下载
用sourcetree拉取5.2.x分支
3. 编译源码
3.1 在spring-framework所在目录执行gradlew build命令
eg: D:\Dev\source\java\spring-framework>gradlew build
3.2 在spring-framework所在目录执行gradlew :spring-oxm:compileTestJava命令
eg: D:\Dev\source\java\spring-framework>gradlew :spring-oxm:compileTestJava
4. 设置IDEA编码
5. 导入IDEA
6. 设置Project Structure
7. 设置gradle任意改动自动编译
8. 对spring-test模块做junit测试
如果选择了test或testNg可能会报错,编辑一下Edit Configurations
清除Tasks
然后重新执行单元测试,选择junit即可
9.编码规范
1. 为了能使自己的新建的module与spring的其他module一起编译,需要遵循google的编码规范
2. Code Style
Code Style · spring-projects/spring-framework Wiki · GitHub
IntelliJ IDEA Editor Settings · spring-projects/spring-framework Wiki · GitHub
10.搭建调试module
拷贝spring-test的gradle配置到sping-debug中进行覆盖,并修改描述为Spring Debug
在spring-debug中创建测试类
/*
* Copyright 2002-2020 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.lazy.bean.test;
/**
* test bean.
*
* @author Lazy Hu
*/
public class MyTestBean {
private String testStr = "testStr";
public String getTestStr() {
return this.testStr;
}
public void setTestStr(String testStr) {
this.testStr = testStr;
}
}
创建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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myTestBean" class="com.lazy.bean.test.MyTestBean"/>
</beans>
创建测试类
在测试类中写入测试信息
/*
* Copyright 2002-2019 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.lazy.bean.test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link MyTestBean}.
*
* @author Lazy Hu
*/
class MyTestBeanTests {
@Test
void testSimpleLoad() {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));
MyTestBean bean = (MyTestBean) bf.getBean("myTestBean");
assertThat(bean.getTestStr()).isEqualTo("testStr");
}
}
进行debug
测试结果
11. 全部模块进行编译
11.1编译
d:
cd Dev\source\java\spring-framework
gradlew build
11.2编译遇到的问题
非法导入 org.junit.jupiter.api.Test;
11.3解决办法
路径(源码所放目录): D:\Dev\source\java\spring-framework\src\checkstyle\checkstyle.xml
修改<property name=”regexp” value=”true” />为<property name=”regexp” value=”false” />
<module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">
<property name="id" value="bannedJUnitJupiterImports"/>
<property name="regexp" value="false" />
<property name="illegalClasses" value="^org\.junit\.jupiter\..+" />
</module>
11.4重新执行编译
阿里云服务器优惠券领取 优惠券
Spring编码规范
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/170007.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...