提交 49031435 编写于 作者: 项羽过江东's avatar 项羽过江东

编译 spring 源码并导入 idea,创建了两个项目验证源码编译成功

上级 03650e7d
# <img src="framework-docs/src/docs/spring-framework.png" width="80" height="80"> Spring Framework [![Build Status](https://ci.spring.io/api/v1/teams/spring-framework/pipelines/spring-framework-6.0.x/jobs/build/badge)](https://ci.spring.io/teams/spring-framework/pipelines/spring-framework-6.0.x?groups=Build") [![Revved up by Gradle Enterprise](https://img.shields.io/badge/Revved%20up%20by-Gradle%20Enterprise-06A0CE?logo=Gradle&labelColor=02303A)](https://ge.spring.io/scans?search.rootProjectNames=spring)
This is the home of the Spring Framework: the foundation for all [Spring projects](https://spring.io/projects). Collectively the Spring Framework and the family of Spring projects are often referred to simply as "Spring".
Spring provides everything required beyond the Java programming language for creating enterprise applications for a wide range of scenarios and architectures. Please read the [Overview](https://docs.spring.io/spring-framework/reference/overview.html) section of the reference documentation for a more complete introduction.
## Code of Conduct
This project is governed by the [Spring Code of Conduct](CODE_OF_CONDUCT.adoc). By participating, you are expected to uphold this code of conduct. Please report unacceptable behavior to spring-code-of-conduct@pivotal.io.
## Access to Binaries
For access to artifacts or a distribution zip, see the [Spring Framework Artifacts](https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Artifacts) wiki page.
## Documentation
The Spring Framework maintains reference documentation ([published](https://docs.spring.io/spring-framework/reference/) and [source](framework-docs/modules/ROOT)), GitHub [wiki pages](https://github.com/spring-projects/spring-framework/wiki), and an
[API reference](https://docs.spring.io/spring-framework/docs/current/javadoc-api/). There are also [guides and tutorials](https://spring.io/guides) across Spring projects.
## Micro-Benchmarks
See the [Micro-Benchmarks](https://github.com/spring-projects/spring-framework/wiki/Micro-Benchmarks) wiki page.
## Build from Source
See the [Build from Source](https://github.com/spring-projects/spring-framework/wiki/Build-from-Source) wiki page and the [CONTRIBUTING.md](CONTRIBUTING.md) file.
## Continuous Integration Builds
Information regarding CI builds can be found in the [Spring Framework Concourse pipeline](ci/README.adoc) documentation.
## Stay in Touch
Follow [@SpringCentral](https://twitter.com/springcentral), [@SpringFramework](https://twitter.com/springframework), and its [team members](https://twitter.com/springframework/lists/team/members) on Twitter. In-depth articles can be found at [The Spring Blog](https://spring.io/blog/), and releases are announced via our [releases feed](https://spring.io/blog/category/releases).
## License
The Spring Framework is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).
# <img src="framework-docs/src/docs/spring-framework.png" width="80" height="80"> Spring Framework [![Build Status](https://ci.spring.io/api/v1/teams/spring-framework/pipelines/spring-framework-6.0.x/jobs/build/badge)](https://ci.spring.io/teams/spring-framework/pipelines/spring-framework-6.0.x?groups=Build") [![Revved up by Gradle Enterprise](https://img.shields.io/badge/Revved%20up%20by-Gradle%20Enterprise-06A0CE?logo=Gradle&labelColor=02303A)](https://ge.spring.io/scans?search.rootProjectNames=spring) 分析 spring 源码,没有什么系统性的分析,就是想看的时候上来看看,纯粹就是为所欲为。
This is the home of the Spring Framework: the foundation for all [Spring projects](https://spring.io/projects). Collectively the Spring Framework and the family of Spring projects are often referred to simply as "Spring". [源码下载](git@gitee.com:mirrors/spring-framework.git)
Spring provides everything required beyond the Java programming language for creating enterprise applications for a wide range of scenarios and architectures. Please read the [Overview](https://docs.spring.io/spring-framework/reference/overview.html) section of the reference documentation for a more complete introduction. 下载完成之后,将源码导入 idea ([源码下载及导入idea](./docs/编译%20spring%20源码.md))
## Code of Conduct
This project is governed by the [Spring Code of Conduct](CODE_OF_CONDUCT.adoc). By participating, you are expected to uphold this code of conduct. Please report unacceptable behavior to spring-code-of-conduct@pivotal.io.
## Access to Binaries
For access to artifacts or a distribution zip, see the [Spring Framework Artifacts](https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Artifacts) wiki page.
## Documentation
The Spring Framework maintains reference documentation ([published](https://docs.spring.io/spring-framework/reference/) and [source](framework-docs/modules/ROOT)), GitHub [wiki pages](https://github.com/spring-projects/spring-framework/wiki), and an
[API reference](https://docs.spring.io/spring-framework/docs/current/javadoc-api/). There are also [guides and tutorials](https://spring.io/guides) across Spring projects.
## Micro-Benchmarks
See the [Micro-Benchmarks](https://github.com/spring-projects/spring-framework/wiki/Micro-Benchmarks) wiki page.
## Build from Source
See the [Build from Source](https://github.com/spring-projects/spring-framework/wiki/Build-from-Source) wiki page and the [CONTRIBUTING.md](CONTRIBUTING.md) file.
## Continuous Integration Builds
Information regarding CI builds can be found in the [Spring Framework Concourse pipeline](ci/README.adoc) documentation.
## Stay in Touch
Follow [@SpringCentral](https://twitter.com/springcentral), [@SpringFramework](https://twitter.com/springframework), and its [team members](https://twitter.com/springframework/lists/team/members) on Twitter. In-depth articles can be found at [The Spring Blog](https://spring.io/blog/), and releases are announced via our [releases feed](https://spring.io/blog/category/releases).
## License
The Spring Framework is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
plugins {
id 'java'
}
group = 'com.matrix.nickel'
version = '6.1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation project(':spring-context')
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
test {
useJUnitPlatform()
}
\ No newline at end of file
package com.matrix.nickel;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* 1. aop 模块,可以使用 gradle 和 jdk 编译 aspectj 的代码,但 gradle 编译出现问题,使用 aspectj 编译
* 2. aspectj 编译,出现问题:ajc: source level should be in '1.1'...'1.8','9'...'16' (or '5.0'..'16.0'): 17
* @author Nickel
* @date 2023-09-03 15:24:31
*/
public class Main {
public static void main(String[] args) {
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
String[] definitionNames = context.getBeanDefinitionNames();
for (String na : definitionNames) {
System.out.println(na);
}
}
}
\ No newline at end of file
package com.matrix.nickel;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author Nickel
* @date 2023-09-03 15:32:12
*/
public class MainTest {
@Test
public void hello(){
System.out.println("中文输出");
}
@Test
public void spring(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
String[] definitionNames = context.getBeanDefinitionNames();
for (String name : definitionNames) {
System.out.println(name);
}
}
}
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
package com.matrix.nickel;
/**
* 下载 maven jar 包
*
* @author Nickel
* @date 2023-09-03 16:11:03
*/
public class MavenJarDownload {
public static void main(String[] args) {
System.out.println("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}
}
}
\ No newline at end of file
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
plugins {
id 'java'
}
group = 'com.matrix.nickel'
version = '6.1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation project(':spring-context')
implementation project(':spring-aop')
implementation project(':spring-aspects')
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation 'org.projectlombok:lombok:1.18.28'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.28'
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.28'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.7'
implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j2-impl', version: '2.20.0'
implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.20.0'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.20.0'
// https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher
// testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
// testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
// testCompile 'org.junit.platform:junit-platform-suite:5.8.2'
// testCompile 'org.junit.jupiter:junit-jupiter-api:5.8.2'
// testCompile 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
// testImplementation 'junit:junit:4.12'
// testCompile group: 'junit', name: 'junit', version: '4.12'
// junit5 单元测试要引入的 包
// testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
// testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
// testImplementation 'org.junit.platform:junit-platform-launcher:1.8.2'
// testImplementation 'org.junit.platform:junit-platform-commons:1.8.2'
// testImplementation 'org.junit.vintage:junit-vintage-engine:5.6.2'
}
test {
useJUnitPlatform()
}
package com.matrix.nickel;
import com.matrix.nickel.aop.HelloConfigOfAOP;
import com.matrix.nickel.aop.MathCalculator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* 1. 构造方法推断
* 2. RootBeanDefinition
*
* @author nickel
* @date 2019/11/10 20:12:10
*/
@Slf4j
public class AopApplication {
public static void main(String[] args) {
// 从AnnotationConfigApplicationContext开始,一步一步的分析spring源码
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(HelloConfigOfAOP.class);
MathCalculator mathCalculator = ctx.getBean(MathCalculator.class);
int divide = mathCalculator.divide(4, 1);
log.info("end: " + divide);
}
}
package com.matrix.nickel.anno.config;
import com.matrix.nickel.anno.service.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
/**
* 1. @Configuration 标记为配置类
* 2. @ComponentScan 配置包扫描:会扫描包路径下的 @Component, @Service, @Repository, @Controller 标记的类
*
* @author Nickel
* @date 2023-06-18 14:39:39
*/
@Configuration
// 排除 Controller 注解的类
// @ComponentScan(value = "com.matrix.nickel.anno.scan", excludeFilters = {
// @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
// })
public class MainConfig {
@Bean
public Person person(){
return new Person("nickel", 33);
}
}
package com.matrix.nickel.anno.scan;
import org.springframework.stereotype.Service;
/**
* @author Nickel
* @date 2023-06-18 17:01:44
*/
@Service
public class PersonService {
}
package com.matrix.nickel.anno.scan;
import org.springframework.stereotype.Controller;
/**
* @author Nickel
* @date 2023-06-18 17:02:31
*/
@Controller
public class StudentController {
}
package com.matrix.nickel.anno.scan;
import org.springframework.stereotype.Repository;
/**
* @author Nickel
* @date 2023-06-18 17:02:18
*/
@Repository
public class StudentDao {
}
package com.matrix.nickel.anno.scan;
import org.springframework.stereotype.Component;
/**
* @author Nickel
* @date 2023-06-18 17:01:57
*/
@Component
public class StudentService {
}
package com.matrix.nickel.anno.service;
/**
* @author Nickel
* @date 2023-06-18 14:43:28
*/
public class HelloService {
}
package com.matrix.nickel.anno.service;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
/**
* @author Nickel
* @date 2023-06-18 14:43:52
*/
@Data
@AllArgsConstructor
@ToString
public class Person {
private String name;
private Integer age;
}
package com.matrix.nickel.aop;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* 1. 导入 aop 模块
* 2. 定义业务逻辑类(MathCalculator):方法执行前后打印日志,如果异常,打印错误日志
* 3. 定义切面类,切入之后要执行的方法
* 4. 给切面定义切入时机(定义织入点)
* 5. 将切面类和目标类加入到容器中
* 6. @Aspect 标记切面类
* 7. 开启切面:@EnableAspectJAutoProxy
* @author Nickel
* @date 2023-06-18 07:35:23
*/
@EnableAspectJAutoProxy
@Configuration
public class HelloConfigOfAOP {
@Bean
public MathCalculator mathCalculator(){
return new MathCalculator();
}
@Bean
public LogAspect logAspect(){
return new LogAspect();
}
}
package com.matrix.nickel.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.*;
/**
* 切面类
* @author Nickel
* @date 2023-06-18 07:39:52
*/
@Slf4j
@Aspect
public class LogAspect {
/**
* 抽取公共的切入点
* 1. 本类引入直接写方法名
* 2. 其他类引入,写全类名就可以了
*/
@Pointcut("execution(public int com.matrix.nickel.aop.MathCalculator.*(..))")
public void pointCut(){}
// 前置通知,方法执行之前切入
@Before("execution(public int com.matrix.nickel.aop.MathCalculator.divide(int, int))")
public void beforeInvoke(){
log.info("before invoke...参数列表:");
}
// 后置通知,方法执行之后切入,不管正常还是异常返回,都会执行
@After("com.matrix.nickel.aop.LogAspect.pointCut()")
public void afterInvoke(){
log.info("after invoke...");
}
// 正常返回通知
@AfterReturning("pointCut()")
public void returnInvoke(){
log.info("method return...返回值:");
}
@AfterThrowing("pointCut()")
public void throwInvoke(){
log.info("method return...发生异常");
}
}
package com.matrix.nickel.aop;
/**
* @author Nickel
* @date 2023-06-18 07:37:44
*/
public class MathCalculator {
public int divide(int x, int y){
return x / y;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
package com.matrix.nickel.anno.config;
import com.matrix.nickel.anno.service.Person;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author Nickel
* @date 2023-06-18 14:44:41
*/
@Slf4j
public class MainConfigTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
Person person = context.getBean(Person.class);
log.info(person.toString());
}
@Test
public void testAnno() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
Person person = context.getBean(Person.class);
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for (String definitionName : beanDefinitionNames) {
log.info(definitionName);
}
}
}
\ No newline at end of file
1. 下载
2. 安装 gradle,下载的版本最好和 spring 源码中定义的一样,源码目录下 gradle/wrapper/gradle-wrapper.properties 中能看到 gradle 版本
添加两个环境变量 GRADLE_HOME, GRADLE_USER_HOME
3. 配置阿里云仓库地址,在配置的 GRADLE_USER_HOME 目录下创建 init.gradle 文件,内容:
```gradle
def repoConfig = {
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url = repo.url.toString()
if (url.contains('repo1.maven.org/maven2') || url.contains('jcenter.bintray.com')) {
println "gradle init: (${repo.name}: ${repo.url}) removed"
remove repo
}
}
}
// maven { url 'http://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
}
allprojects {
buildscript {
repositories repoConfig
}
repositories repoConfig
}
```
4. 编译 gradlew :spring-oxm:compileTestJava
5. 如果报错: Failed to calculate the value of task ':spring-core:compileJava21Java' property 'javaCompiler',修改 spring-core 目录下的 spring-core.gradle
```
multiRelease {
// targetVersions 17, 21
targetVersions 17
}
```
注意,使用 jdk17 编译。
\ No newline at end of file
pluginManagement { pluginManagement {
repositories { repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
mavenCentral() mavenCentral()
gradlePluginPortal() gradlePluginPortal()
maven { url "https://repo.spring.io/release" } maven { url "https://repo.spring.io/release" }
...@@ -59,3 +60,6 @@ settings.gradle.projectsLoaded { ...@@ -59,3 +60,6 @@ settings.gradle.projectsLoaded {
} }
} }
} }
include '_01-hellospring'
include '_03-aop-demo'
...@@ -12,7 +12,9 @@ dependencies { ...@@ -12,7 +12,9 @@ dependencies {
api(project(":spring-core")) api(project(":spring-core"))
api(project(":spring-expression")) api(project(":spring-expression"))
api("io.micrometer:micrometer-observation") api("io.micrometer:micrometer-observation")
optional(project(":spring-instrument")) // 通过 idea 运行程序(不是通过 gradle 运行,在 gradle 中设置)时,找不到符号
// optional(project(":spring-instrument"))
api(project(":spring-instrument"))
optional("io.projectreactor:reactor-core") optional("io.projectreactor:reactor-core")
optional("jakarta.annotation:jakarta.annotation-api") optional("jakarta.annotation:jakarta.annotation-api")
optional("jakarta.ejb:jakarta.ejb-api") optional("jakarta.ejb:jakarta.ejb-api")
......
...@@ -11,7 +11,8 @@ apply plugin: "kotlin" ...@@ -11,7 +11,8 @@ apply plugin: "kotlin"
apply plugin: "kotlinx-serialization" apply plugin: "kotlinx-serialization"
multiRelease { multiRelease {
targetVersions 17, 21 // targetVersions 17, 21
targetVersions 17
} }
def javapoetVersion = "1.13.0" def javapoetVersion = "1.13.0"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册