环境准备
下载并编译
- 打开Spring官网,选择github地址
- 选择合适下载方式,可以下载zip包,也可以用git clone
- 进入下载后的目录,切换到5.2.x分支,在build.gradle修改阿里云镜像配置具体参考
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
}
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
- 项目导入idea,文档地址
4.1 Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
(在控制台预编译spring-oxm,控制台执行./gradlew :spring-oxm:compileTestJava)
4.2 Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
(idea导入项目)
4.3 When prompted exclude the spring-aspects module (or after the import via File-> Project Structure -> Modules)
(排除spring-aspects模块,AspectJ编译器Ajc后续在aop会下载这个编译器)
4.4 Code away
- 可以在编码前需要去掉src下的checkstyle文件夹下的checkstyle.xml规则,具体参考
编码
- 首先在spring-framework新建spring-demo模块,并在build.gradle引入context依赖
plugins {
id 'java'
}
group 'org.springframework'
version '5.2.17.BUILD-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile(project(":spring-context"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
- 在src\main新建com.imooc.service包并新建WelcomeService接口和对应实现类WelcomeServiceImpl
package com.imooc.service;
public interface WelcomeService {
String sayHello(String name);
}
public class WelcomeServiceImpl implements WelcomeService {
@Override
public String sayHello(String name) {
System.out.println("欢迎您:" + name);
return "欢迎您:" + name;
}
}
- 在src\main\resource\spring新建spring-config.xml,配置相关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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="welcomeService" class="com.imooc.service.impl.WelcomeServiceImpl"/>
</beans>
- 新建入口类
public class Entrance {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:spring/spring-config.xml");
WelcomeService welcomeService = applicationContext.getBean(WelcomeService.class);
welcomeService.sayHello("spring xml方式获取bean");
}
}
- 运行Entrace的main方法,控制台输出
欢迎您:spring xml方式获取bean
- 至此下载编译运行源码成功
小结
- 编译源码需要下载大量包,如果有下载失败可以重复多下载几次
- 需要使用高版本的idea,获取高版本kotlin语法支持
网友评论