先从github下载Spring
https://github.com/spring-projects/spring-framework
然后选择版本,此处用5.2.7RELEASE
构造工具
GradleWraper中的Gradle:
Gradle在spring-framework-5.2.7.RELEASE\gradle\wrapper\gradle-wrapper.properties文件中有,相当于远程自动下载gradle到本地。
修改build.gradle :
这个文件就相当于我们Maven的pom.xml 管理项目的依赖等
repositories {
maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'}
maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
开始编译
编译compileTestJava模块
打开源码所在文件夹,在windows cmd命令中输入
gradlew :spring-oxm:compileTestJava
成功后,导入项目到idea中
导入后等待编译
添加测试模块代码:
new->model->gradle–>输入模块名称
添加依赖
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile(project(":spring-context"))
}
compile(project(":spring-context")) 代表本项目的
随意添加任意bean
@Service
public class MyServiceImpl {
public void say(){
System.out.println("hello world");
}
}
启动
@Component
@ComponentScan("com.test")
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Test.class);
MyServiceImpl bean = (MyServiceImpl)applicationContext.getBean("myServiceImpl");
bean.say();
}
}
成功输出