環境
- mac電腦
- Idea(2018版)
- Jdk1.8
- Gradle4.10.3
- spring-framework(v5.1.6)
步驟
2、根據Spring下載指定版本gradle
打開spring源碼目錄下gradle/wrapper/gradle-wrapper.properties可以看到需要下載的gradle版本
3、下載gradle
- 下載后解壓到指定目錄
- 修改環境變量
- 打開終端輸入$: vim ~/.bash_profile
-
使環境變量生效
- 終端輸入: source ~/.bash_profile
- 驗證
- 終端輸入:gradle -v
4、給gradle配置阿里雲鏡像
- 在gradle下的init.d文件夾內創建init.gradle文件
-
加入如下:
allprojects{
repositories {
def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
remove repo
}
}
}
maven {
url REPOSITORY_URL
}
}
}
5、導入spring源碼
5.1
5.2
接下來等待idea自動幫我們編譯完成即可
6、完成狀態
7、測試
目錄結構如下
注意:這里的代碼需要分別導入對應的文件
//1、MyBean public class MyBean { private String name = "Adom"; public MyBean(String name) { this.name = name; } public MyBean() { } @Override public String toString() { return "MyBean{" + "name='" + name + '\'' + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } } //2、Spring-config.xml <?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="com.adom.MyBean"/> </beans>
//3、MyBeanTest /** * MyBean Tester. * * @author <Authors name> * @since <pre>Jul 27, 2020</pre> * @version 1.0 */ public class MyBeanTest { @Test public void testMyBean(){ BeanFactory bf = new XmlBeanFactory( new ClassPathResource("spring-config.xml")); MyBean myTestBean = (MyBean) bf.getBean("myBean"); System.out.println(myTestBean.getName()); } } //4、Build.gradle dependencies { compile(project(":spring-beans")) compile(project(":spring-core")) testCompile group: 'junit', name: 'junit', version: '4.12' }
最后點擊運行
遇到的問題
Spring-core模塊核心代碼報錯
原因
在spring源碼根目錄執行以下命令解決,gradle構建時缺少spring-cglib-repack、spring-objenesis-repack缺少jar依賴的問題
解決方法
在終端執行如下命令:
gradle cglibRepackJar
gradle objenesisRepackJar