环境:
- jdk1.8
- spring 5.20
- IDEA 2019
下载
-
选择spring framework,进入github
-
选择稳定版下载
编译
-
参考官方教程:https://github.com/spring-projects/spring-framework/blob/main/import-into-idea.md
github界面选择:
在wiki的界面翻到最后:
-
按照step里面的步骤进行
使用gradlew编译,添加下述语句
# 修改的第一部分 repositories { maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/' } maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'} } # 修改的第而部分 maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'} maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
-
保存后,在spring框架目录下打开cmd执行
./gradlew :spring-oxm:compileTestJava
(如果是windows的话,执行.\gradlew :spring-oxm:compileTestJava
或gradlew :spring-oxm:compileTestJava
) -
IDEA import项目
-
把spring-aspect给upload出去,然后重新编译
测试使用
-
新建module,注意选择gradle创建(maven和gradle共存会有问题)
-
新建package和class
最终的文件结构如图:
-
Service实现
接口:package com.mytest.service; public interface WelcomeService { String sayHello(String name); }
实现类:
package com.mytest.service.impl; import com.mytest.service.WelcomeService; public class WelcomeServiceImpl implements WelcomeService { @Override public String sayHello(String name) { System.out.println("欢迎"); System.out.println(name); return "success"; } }
-
resource里的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="welcomeService" class="com.mytest.service.impl.WelcomeServiceImpl"/> </beans>
-
gradle
plugins { id 'java' } group 'org.springframework' version '5.2.0.RELEASE' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile(project(":spring-context")) testCompile group: 'junit', name: 'junit', version: '4.12' }
新加了语句:
compile(project(":spring-context"))
,加完后在IDEA右下角,自动导入
-
入口函数Entrance
package com.mytest; import com.mytest.service.WelcomeService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Entrance { public static void main(String[] args) { String xmlPath = "D:\\spring-framework-5.2.0.RELEASE\\springdemo\\src\\main\\resources\\spring\\spring-config.xml"; ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath); WelcomeService welcomeService = (WelcomeService) applicationContext.getBean("welcomeService"); welcomeService.sayHello("啦啦啦,a test"); } }
然后点击运行就可以了。
报错:
Error:(347, 51) java: 找不到符号
符号: 变量 CoroutinesUtils
位置: 类 org.springframework.core.ReactiveAdapterRegistry.CoroutinesRegistrar
解决方法:
点击File -> Project Structure -> Libraries -> + -> Java,然后选择spring-framework/spring-core/kotlin-coroutines/build/libs/kotlin-coroutines-5.2.4.BUILD-SNAPSHOT.jar,在弹出的对话框中选择spring-core.main,在重新build项目即可。