参考网上各种资源,终于把spring源码运行起来了;步骤总结如下:
spring版本: 5.2.x
本地系统mac
idea 2019.3.2 下载地址:https://www.jetbrains.com/idea/download/other.html
jdk 11 下载地址:https://repo.huaweicloud.com/java/jdk/
1. 前期准备
安装git、gradle;git的安装过程相对简单,这里只说gradle的安装;
在spring的github中可以看到需要安装的gradle的版本:
下载gradle 5.6.4:https://services.gradle.org/distributions/gradle-5.6.4-bin.zip
下载完毕之后,开始配置环境变量:
vim ~/.bash_profile
export PATH=$PATH:/opt/gradle/gradle-7.5.1/bin
source ~/.bash_profile
2. 下载spring源码
下载地址:https://gitcode.net/mirrors/spring-projects/spring-framework/-/tree/5.2.x/
下载到本地之后,解压;
3. 导入spring源码
通过File->Open的方式直接导入spring源码;
导入成功之后,如果未关联git会报如下错误:
org.gradle.process.internal.ExecException: Process ‘command ‘git’’ finished with non-zero exit value 128
操作如下:
git init .
git add .
git commit -am ‘init’
通过以上命令就可以解决;
修改gradle的配置:
- build.gradle,增加阿里云镜像库
maven { url “https://maven.aliyun.com/nexus/content/groups/public/” }
maven { url “https://maven.aliyun.com/nexus/content/repositories/jcenter”}
2. settings.gradle
maven { url “https://maven.aliyun.com/nexus/content/groups/public/” }
3. gradle.properties
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
增加VM Options
-Dfile.encoding=UTF-8
配置好以上,在控制台执行:
./gradlew :spring-oxm:compileTestJava
然后执行;
4. 验证
创建一个新module spring-demo
修改gradle名称
并向gradle中追加:
dependencies {
compile(project(“:spring-context”))
testCompile group: ‘junit’, name: ‘junit’, version: ‘4.12’
}
编写测试类:
public static void main(String[] args) {
final AnnotationConfigApplicationContext ann = new AnnotationConfigApplicationContext("com.sxl");
final Student student = (Student) ann.getBean("student");
student.say();
}
运行结果:
5. 遇到的错
-
org.gradle.process.internal.ExecException: Process ‘command ‘git’’ finished with non-zero exit value 128
将spring项目添加到git中就解决了; -
Error:Kotlin: warnings found and -Werror specified
两种方式:1
如果还是报错,则:
则找到所有的Kotin将其 -Werror去掉; -
Error:Kotlin: [Internal Error] java.lang.LinkageError: loader constraint violation: loader org.jetbrains.kotlin.cli.jvm.plugins.PluginURLClassLoader S e l f T h e n P a r e n t U R L C l a s s L o a d e r @ 53832173 ( i n s t a n c e o f o r g . j e t b r a i n s . k o t l i n . c l i . j v m . p l u g i n s . P l u g i n U R L C l a s s L o a d e r SelfThenParentURLClassLoader @53832173 (instance of org.jetbrains.kotlin.cli.jvm.plugins.PluginURLClassLoader SelfThenParentURLClassLoader@53832173(instanceoforg.jetbrains.kotlin.cli.jvm.plugins.PluginURLClassLoaderSelfThenParentURLClassLoader, child of ‘bootstrap’) wants to load interface kotlin.sequences.Sequence. A different interface with the same name was previously loaded by ‘app’ (instance of jdk.internal.loader.ClassLoaders$AppClassLoader).
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:550)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
at java.base/java.net.URLClassLoader 1. r u n ( U R L C l a s s L o a d e r . j a v a : 452 ) a t j a v a . b a s e / j a v a . s e c u r i t y . A c c e s s C o n t r o l l e r . d o P r i v i l e g e d ( N a t i v e M e t h o d ) a t j a v a . b a s e / j a v a . n e t . U R L C l a s s L o a d e r . f i n d C l a s s ( U R L C l a s s L o a d e r . j a v a : 451 ) a t o r g . j e t b r a i n s . k o t l i n . c l i . j v m . p l u g i n s . P l u g i n U R L C l a s s L o a d e r 1.run(URLClassLoader.java:452) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451) at org.jetbrains.kotlin.cli.jvm.plugins.PluginURLClassLoader 1.run(URLClassLoader.java:452)atjava.base/java.security.AccessController.doPrivileged(NativeMethod)atjava.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)atorg.jetbrains.kotlin.cli.jvm.plugins.PluginURLClassLoaderSelfThenParentURLClassLoader.findClass(PluginURLClassLoader.kt:47)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:550)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
at java.base/java.net.URLClassLoader 1. r u n ( U R L C l a s s L o a d e r . j a v a : 452 ) a t j a v a . b a s e / j a v a . s e c u r i t y . A c c e s s C o n t r o l l e r . d o P r i v i l e g e d ( N a t i v e M e t h o d ) a t j a v a . b a s e / j a v a . n e t . U R L C l a s s L o a d e r . f i n d C l a s s ( U R L C l a s s L o a d e r . j a v a : 451 ) a t o r g . j e t b r a i n s . k o t l i n . c l i . j v m . p l u g i n s . P l u g i n U R L C l a s s L o a d e r 1.run(URLClassLoader.java:452) at java.base/java.security.AccessController.doPrivileged(Native Method) at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451) at org.jetbrains.kotlin.cli.jvm.plugins.PluginURLClassLoader 1.run(URLClassLoader.java:452)atjava.base/java.security.AccessController.doPrivileged(NativeMethod)atjava.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)atorg.jetbrains.kotlin.cli.jvm.plugins.PluginURLClassLoaderSelfThenParentURLClassLoader.findClass(PluginURLClassLoader.kt:47)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at kotlin.coroutines.experimental.SequenceBuilderKt__SequenceBuilderKt.buildSequence(SequenceBuilder.kt:24)
at org.jetbrains.kotlin.scripting.compiler.plugin.ScriptiDefinitionsFromClasspathDiscoverySourceKt.discoverScriptTemplatesInClasspath(ScriptiDefinitionsFromClasspathDiscoverySource.kt:53)
at org.jetbrains.kotlin.scripting.compiler.plugin.ScriptDefinitionsFromClasspathDiscoverySource.(ScriptiDefinitionsFromClasspathDiscoverySource.kt:37)
at org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationExtension.updateConfiguration(ScriptingCompilerConfigurationExtension.kt:56)
我是通过升级idea版本来解决的,最初是用的2018,升级到了2019解决了该问题;