环境搭建
# 环境搭建
https://github.com/spring-projects/spring-framework/wiki/Build-from-Source (opens new window)
不用下载gradle,idea会自动帮我们下载
只用配置gradle安装依赖的位置
GRADLE_USER_HOME
相当于maven依赖存放的位置
拉取代码
# 版本选择 5.1.x
git clone -b 5.1.x https://github.com/spring-projects/spring-framework.git
idea打开项目,自动帮我们下载gradle(下载的版本为spring-framework/gradle/wrapper/gradle-wrapper.properties
中的配置版本)并下载项目所需的依赖,依赖大小共2G+,如果下载慢的话,可以挂VPN(或则配置阿里云镜像)
下载完依赖后,新建一个gradle模块,进行测试
测试模块结构
添加依赖build.gradle
plugins {
id 'java'
}
group 'org.springframework'
version '5.1.21.BUILD-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
// 导入依赖
compile(project(":spring-context"))
// 简化配置 lombok
implementation 'org.projectlombok:lombok:1.18.22'
}
test {
useJUnitPlatform()
}
简单对象
@Data
public class Person {
private String name;
}
创建spring经典的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 class="com.starry.spring.bean.Person" id="person">
<property name="name" value="starry"/>
</bean>
</beans>
测试类
public class MainTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Person person = applicationContext.getBean(Person.class);
System.out.println("### person.getName() = " + person.getName());
}
}
main方法成功运行,环境搭建成功
### person.getName() = starry
上次更新: 2024/03/03, 08:36:37