文章目录
环境准备
JDK11、Spring Framework 5.2.0 release
编译
修改build.gradle
在这主要将仓库修改为阿里云镜像(在开头buildscript 中添加,笔者一开始下载的是5.3.22版本,该文件里没有这个字段),gradle虽然没手动安装但是在编译时可直接快速下载。
buildscript {
repositories{
maven { url'https://maven.aliyun.com/repository/public/' }
maven { url'https://maven.aliyun.com/repository/jcenter' }
}
dependencies {
classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16'
classpath 'io.spring.asciidoctor:spring-asciidoctor-extensions:0.1.3.RELEASE'
}
}
编译
官网教程地址Build from Source
根据官网的相关说明,最下面有Eclipse和IntelliJ IDEA的导入方法,但是JDK版本要看你用的spring版本对应的要求,官网的应该是显示最新的,版本不一致会预编译失败。
- 进入spring根目录运行
gradlew :spring-oxm:compileTestJava
- 通过idea
(File ->New -> Project from Existing Sources -> Navigate to directory ->Select build.gradle)
导入项目 - 移除
spring-aspects
模块,移除后idea重新构建就OK了
测试
创建一个下项目测试是否能将其他模块作为jar包导入
项目结构如下
定义主类Entrance
public class Entrance {
public static void main(String[] args) {
System.out.println("Hello world!");
String xmlPath = "E:\\Git\\Spring-test\\spring-framework-5.2.0.RELEASE\\springdemo\\src\\main\\resources\\spring\\spring-config.xml";
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath); //返回spring容器
WelcomeServcie welcomeServcie = (WelcomeServcie) applicationContext.getBean("welcomeService");
welcomeServcie.sayHello("第一个sping");
}
}
定义接口WelcomeServcie
public interface WelcomeServcie {
String sayHello(String name);
}
定义接口实现类WelcomeServcieimpl
public class WelcomeServcieimpl implements WelcomeServcie {
@Override
public String sayHello(String name) {
System.out.println("欢迎!:"+name);
return "success";
}
}
配置xml
导入已有的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.WelcomeServcieimpl"/>
</beans>
修改项目中的build.gradle
导入spring-context,在dependencies添加一行就行,这里的compile方法在后续的版本中弃用了,
plugins {
id 'java'
}
group 'org.springframework'
version '5.2.0.RELEASE'
repositories {
mavenCentral()
}
dependencies {
compile(project(":spring-context"))
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
test {
useJUnitPlatform()
}