前面我们构建好了Spring源码,接下来肯定迫不及待来调试啦,来一起看看大名鼎鼎
ApplicationContext
新建模块
1、基础步骤
2、重要文件
build.gradle
plugins {
id 'java'
}
group = 'org.springframework'
version = '5.2.6.RELEASE'
repositories {
mavenCentral()
}
// 引入依赖
dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
compile(project(":spring-context"))
optional(project(":spring-context"))
}
test {
useJUnitPlatform()
}
setting.gradle
pluginManagement {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
gradlePluginPortal()
maven { url 'https://repo.spring.io/plugins-release' }
}
}
apply from: "$rootDir/gradle/build-cache-settings.gradle"
include "spring-aop"
include "spring-aspects"
include "spring-beans"
include "spring-context"
include "spring-context-support"
include "spring-context-indexer"
include "spring-core"
include "kotlin-coroutines"
project(':kotlin-coroutines').projectDir = file('spring-core/kotlin-coroutines')
include "spring-expression"
include "spring-instrument"
include "spring-jcl"
include "spring-jdbc"
include "spring-jms"
include "spring-messaging"
include "spring-orm"
include "spring-oxm"
include "spring-test"
include "spring-tx"
include "spring-web"
include "spring-webmvc"
include "spring-webflux"
include "spring-websocket"
include "framework-bom"
include "integration-tests"
rootProject.name = "spring"
rootProject.children.each {project ->
project.buildFileName = "${project.name}.gradle"
}
include 'spring-self'
spring-self.gradle
有个小细节,如果想和spring源码的gradle保持一致,重点看这段代码:
rootProject.name = "spring"
rootProject.children.each {project ->
project.buildFileName = "${project.name}.gradle"
}
此时我们只要将include 'spring-self' 放到这段代码上方即可,这样我们的build.gradle 就可以改成与spring同样的命令风格spring-self.gradle
3、代码编写
代码目录
实体类
public class JmUser {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
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="jmUser" class="org.springframework.dto.JmUser">
<property name="name" value="测试一" />
<property name="age" value="18" />
</bean>
</beans>
主方法
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
JmUser jmUser = (JmUser)context.getBean("jmUser");
System.out.println(jmUser.getName());
System.out.println(jmUser.getAge());
}
}