java不同版本特性示例

This commit is contained in:
maxf
2025-08-05 18:53:13 +08:00
parent 5c87319677
commit a4be814442
66 changed files with 1619 additions and 101 deletions

View File

@@ -7,7 +7,9 @@ java 知识点学习
3. 文件读取
4. 动静态资源
5. 模板渲染
2. `@Vaild`为什么能做到校验参数?
6. 扩展:[README.md](demo1-V2/README.md)
2. [java各个版本的特性](demo2/README.md)
3. `@Vaild`为什么能做到校验参数?
1. 请求参数处理
2. 切面AOP
3. 目标寻址请求URL到目标方法ctrl
@@ -16,7 +18,7 @@ java 知识点学习
6. [POST] multipart/form-data文件上传
7. [POST] application/jsonJSON数据
8. [POST] text/xmlXML数据
3. spring boot的启动原理
4. spring boot的启动原理
1.maven的版本如何在中央仓库中查看
2.jsp漏洞问题带来的问题

View File

@@ -1,6 +1,6 @@
从0到1实现一个web服务器
从0到1实现一个web服务器:扩展
---
### 实现https访问
### 一、实现https访问
1. 生成证书
1. 方式1使用openssl生成证书
```shell
@@ -14,7 +14,7 @@
Verifying - Enter Export Password: (123456)
```
2. 代码参照: [top.yexuejc.demo.core.WebServer](src/main/java/top/yexuejc/demo/core/WebServer.java).startHttps
### 扩展一个ctrl能正常处理请求
### 二、扩展一个ctrl能正常处理请求
1. 定义[@RestController](src/main/java/top/yexuejc/demo/annotation/RestController.java)和[@GetMapping](src/main/java/top/yexuejc/demo/annotation/GetMapping.java)注解
2. 定义扫描器和模拟bean容器: [ControllerSupplier](src/main/java/top/yexuejc/demo/core/ControllerSupplier.java)
3. 接入处理逻辑: [RequestHandler](src/main/java/top/yexuejc/demo/core/RequestHandler.java) line 49
@@ -25,8 +25,8 @@ Response response = ControllerSupplier.invoke(request);
```java
ControllerSupplier.builder(WebServerApplication.class);
```
### 启动参数配置
### 三、启动参数配置
参照 [WebServerApplication](src/main/java/top/yexuejc/demo/WebServerApplication.java).argsProcess
### 配置文件读取
### 四、配置文件读取
参照 [AppConfig](src/main/java/top/yexuejc/demo/AppConfig.java)

View File

@@ -8,4 +8,27 @@
<name>demo1</name>
<description>从0到1实现一个web服务器</description>
<properties>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<compilerArgs>
<!-- 开启预览功能STR模板引擎-->
<arg>--enable-preview</arg>
</compilerArgs>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

38
demo2/README.md Normal file
View File

@@ -0,0 +1,38 @@
java各个版本的特性
---
### **Java 8 (2014) - LTS**
- **Lambda表达式**:简化函数式编程[1][3]
- **Stream API**:支持函数式数据流处理[1]
- **接口默认方法与静态方法**:允许接口包含实现[1]
- **新的日期时间APIjava.time**替代旧的Date/Calendar类[1]
- **Optional类**减少NullPointerException风险[1]
### **Java 9 (2017)**
- **模块化系统Jigsaw**:引入模块化编程[2]
- **JShell**交互式REPL工具[2]
### **Java 10 (2018)**
- **局部变量类型推断var**:简化局部变量声明[2]
### **Java 11 (2018) - LTS**
- **HTTP Client API**标准化HTTP客户端[2]
- **单文件源码直接运行**:无需编译即可执行`.java`文件[2]
### **Java 12-17非LTS版本**
- **Switch表达式增强**Java 12/13/14逐步完善[2]
- **文本块Text Blocks**简化多行字符串Java 13预览Java 15正式[2]
- **Records**简化不可变数据类Java 14预览Java 16正式[2]
- **Sealed Classes**限制类继承Java 15预览Java 17正式[2]
### **Java 18-21**
- **模式匹配增强**instanceof、switch模式匹配[2]
- **虚拟线程(预览)**轻量级并发模型Java 19预览Java 21正式[2]
### **Java 22-24部分特性为预览或实验性**
- **分代Shenandoah垃圾收集器**Java 24[4]
- **紧凑对象标头**优化内存使用Java 24[4]
- **限制JNI使用**提升安全性Java 24[4]
- **G1垃圾收集器扩展**Java 24[4]

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java10-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>10</java.version>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java11-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java12-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>12</java.version>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java13-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>13</java.version>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java14-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>14</java.version>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java15-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>15</java.version>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java16-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>16</java.version>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java17-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java18-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>18</java.version>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java19-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>19</java.version>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java20-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>20</java.version>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java21-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java22-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>22</java.version>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java23-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>23</java.version>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java24-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>24</java.version>
<maven.compiler.source>24</maven.compiler.source>
<maven.compiler.target>24</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,9 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
}
}

View File

@@ -0,0 +1,141 @@
Java 8 特性示例项目
---
## 项目结构
```
src/main/java/
├── Main.java // 主程序入口
├── LambdaExample.java // Lambda表达式示例
├── StreamExample.java // Stream API示例
├── OptionalExample.java // Optional类示例
├── DateTimeExample.java // 新日期时间API示例
└── FunctionExample.java // 函数式接口示例
```
## Java 8 特性详解
### 1. Lambda 表达式 (LambdaExample.java)
Lambda表达式是Java 8最重要的新特性之一它允许把函数作为一个方法的参数函数作为参数传递
主要示例:
- Runnable接口的Lambda简化写法
- 集合排序的Lambda表达式用法
- 不同形式的Lambda表达式语法
```
java
// 传统匿名内部类
Runnable oldRunnable = new Runnable() {
@Override
public void run() {
System.out.println("传统方式执行Runnable");
}
};
// Lambda表达式
Runnable newRunnable = () -> System.out.println("Lambda方式执行Runnable");
```
### 2. Stream API (StreamExample.java)
Stream API是Java 8中处理集合的关键抽象概念它可以对集合进行各种操作如过滤、映射、排序等。
主要示例:
- filter() 过滤操作
- map() 映射操作
- collect() 收集操作
- findFirst() 查找操作
- parallelStream() 并行流处理
```
java
// 过滤年龄大于25的人
List<Person> filtered = people.stream()
.filter(person -> person.getAge() > 25)
.collect(Collectors.toList());
// 提取所有人的姓名
List<String> names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
```
### 3. Optional 类 (OptionalExample.java)
Optional类是一个可以为null的容器对象用来避免空指针异常更好地处理可能为null的值。
主要示例:
- of() 和 empty() 创建Optional对象
- isPresent() 判断是否有值
- orElse() 提供默认值
- ifPresent() 存在时执行操作
- map() 转换值
```
java
Optional<String> optional = Optional.of("Hello Java 8");
Optional<String> emptyOptional = Optional.empty();
// 使用orElse提供默认值
String value = emptyOptional.orElse("默认值");
// 使用ifPresent执行操作
optional.ifPresent(s -> System.out.println("处理值: " + s.toUpperCase()));
```
### 4. 新的日期时间API (DateTimeExample.java)
Java 8引入了新的日期时间APIjava.time包解决了之前Date和Calendar类的缺陷。
主要示例:
- LocalDate 只包含日期
- LocalDateTime 包含日期和时间
- DateTimeFormatter 日期格式化
- plusWeeks(), minusMonths() 日期计算
```
java
// LocalDate示例
LocalDate today = LocalDate.now();
LocalDate specificDate = LocalDate.of(2025, 7, 18);
// LocalDateTime示例
LocalDateTime now = LocalDateTime.now();
// 日期格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = now.format(formatter);
```
### 5. 函数式接口 (FunctionExample.java)
Java 8提供了许多内置的函数式接口如Predicate、Consumer、Function、Supplier等。
主要示例:
- Predicate 用于判断操作
- Consumer 用于消费数据
- Function 用于数据转换
- Supplier 用于生成数据
```
java
// Predicate示例
Predicate<Integer> isEven = n -> n % 2 == 0;
// Consumer示例
Consumer<String> printer = System.out::println;
// Function示例
java.util.function.Function<String, Integer> stringToLength = String::length;
// Supplier示例
java.util.function.Supplier<Double> randomSupplier = Math::random;
```
## 学习建议
建议按以下顺序学习Java 8特性
1. 先理解Lambda表达式的基本语法
2. 学习函数式接口的概念
3. 掌握Stream API的常用操作
4. 熟悉Optional类的使用场景
5. 了解新的日期时间API的优势

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java8-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,40 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author maxiaofeng
* @date 2025/8/5 13:53
*/
public class DateTimeExample {
/**
* 新的日期时间API示例
* Java 8引入了新的日期时间API解决了之前Date和Calendar类的缺陷
*/
static void dateTimeExample() {
System.out.println("\n=== 新日期时间API示例 ===");
// LocalDate示例 - 只包含日期
LocalDate today = LocalDate.now();
System.out.println("今天的日期: " + today);
LocalDate specificDate = LocalDate.of(2025, 7, 18);
System.out.println("指定日期: " + specificDate);
// LocalDateTime示例 - 包含日期和时间
LocalDateTime now = LocalDateTime.now();
System.out.println("当前日期时间: " + now);
// 日期格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = now.format(formatter);
System.out.println("格式化后的日期时间: " + formattedDate);
// 日期计算
LocalDate nextWeek = today.plusWeeks(1);
System.out.println("下周的日期: " + nextWeek);
LocalDate lastMonth = today.minusMonths(1);
System.out.println("上个月的日期: " + lastMonth);
}
}

View File

@@ -0,0 +1,36 @@
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
* @author maxiaofeng
* @date 2025/8/5 13:53
*/
public class FunctionExample {
/**
* 函数式接口示例
* Java 8提供了许多内置的函数式接口
*/
static void functionalInterfaceExample() {
System.out.println("\n=== 函数式接口示例 ===");
// Predicate示例 - 用于判断
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println("10是偶数吗? " + isEven.test(10));
System.out.println("11是偶数吗? " + isEven.test(11));
// Consumer示例 - 用于消费数据
Consumer<String> printer = System.out::println;
printer.accept("使用Consumer打印信息");
// Function示例 - 用于转换
java.util.function.Function<String, Integer> stringToLength = String::length;
int length = stringToLength.apply("Java 8");
System.out.println("字符串'Java 8'的长度: " + length);
// Supplier示例 - 用于生成数据
java.util.function.Supplier<Double> randomSupplier = Math::random;
System.out.println("随机数: " + randomSupplier.get());
}
}

View File

@@ -0,0 +1,51 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author maxiaofeng
* @date 2025/8/5 13:51
*/
public class LambdaExample {
/**
* Lambda表达式示例
* Lambda表达式是Java 8最重要的新特性之一它允许把函数作为一个方法的参数
*/
public static void lambdaExample() {
System.out.println("\n=== Lambda表达式示例 ===");
// 传统的匿名内部类写法
Runnable oldRunnable = new Runnable() {
@Override
public void run() {
System.out.println("传统方式执行Runnable");
}
};
// Lambda表达式写法
Runnable newRunnable = () -> System.out.println("Lambda方式执行Runnable");
oldRunnable.run();
newRunnable.run();
// 集合排序的Lambda表达式示例
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
// 传统方式
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return a.compareTo(b);
}
});
// Lambda方式
Collections.sort(names, (a, b) -> a.compareTo(b));
// 更简洁的Lambda方式
names.sort((a, b) -> b.compareTo(a)); // 逆序排列
System.out.println("Lambda排序结果: " + names);
}
}

View File

@@ -0,0 +1,26 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
// 1. Lambda表达式示例
LambdaExample.lambdaExample();
// 2. Stream API示例
StreamExample.streamExample();
// 3. Optional示例
OptionalExample.optionalExample();
// 4. 新的日期时间API示例
DateTimeExample.dateTimeExample();
// 5. 函数式接口示例
FunctionExample.functionalInterfaceExample();
}
}

View File

@@ -0,0 +1,35 @@
import java.util.Optional;
/**
* @author maxiaofeng
* @date 2025/8/5 13:52
*/
public class OptionalExample {
/**
* Optional示例
* Optional类是一个可以为null的容器对象用来避免空指针异常
*/
public static void optionalExample() {
System.out.println("\n=== Optional示例 ===");
// 创建Optional对象
Optional<String> optional = Optional.of("Hello Java 8");
Optional<String> emptyOptional = Optional.empty();
// 判断是否有值
if (optional.isPresent()) {
System.out.println("Optional中的值: " + optional.get());
}
// 使用orElse提供默认值
String value = emptyOptional.orElse("默认值");
System.out.println("Optional默认值: " + value);
// 使用ifPresent执行操作
optional.ifPresent(s -> System.out.println("处理值: " + s.toUpperCase()));
// 使用map转换值
Optional<String> upperCase = optional.map(String::toUpperCase);
upperCase.ifPresent(s -> System.out.println("转换后的值: " + s));
}
}

View File

@@ -0,0 +1,83 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author maxiaofeng
* @date 2025/8/5 13:52
*/
public class StreamExample {
/**
* Stream API示例
* Stream API是Java 8中处理集合的关键抽象概念它可以对集合进行各种操作
*/
public static void streamExample() {
System.out.println("\n=== Stream API示例 ===");
List<Person> people = Arrays.asList(
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Charlie", 35),
new Person("David", 40),
new Person("Eve", 20)
);
// 过滤年龄大于25的人
List<Person> filtered = people.stream()
.filter(person -> person.getAge() > 25)
.collect(Collectors.toList());
System.out.println("年龄大于25的人: " + filtered);
// 提取所有人的姓名
List<String> names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
System.out.println("所有人的姓名: " + names);
// 计算年龄总和
int totalAge = people.stream()
.mapToInt(Person::getAge)
.sum();
System.out.println("年龄总和: " + totalAge);
// 查找第一个年龄大于30的人
Optional<Person> person = people.stream()
.filter(p -> p.getAge() > 30)
.findFirst();
person.ifPresent(p -> System.out.println("第一个年龄大于30的人: " + p));
// 并行流示例
long count = people.parallelStream()
.filter(p -> p.getAge() > 25)
.count();
System.out.println("年龄大于25的人数: " + count);
}
/**
* 人员实体类
*/
public static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
}

View File

@@ -0,0 +1,191 @@
## 1. HTTP/2 Client (孵化阶段)
Java 9引入了新的HTTP客户端API支持HTTP/2和WebSocket位于`jdk.incubator.http`包中。
```java
// 示例代码
import jdk.incubator.http.HttpClient;
import jdk.incubator.http.HttpRequest;
import jdk.incubator.http.HttpResponse;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://example.com")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
```
## 2. Multi-Resolution Images API
支持多分辨率图像处理可以处理不同DPI的图像。
```java
// 示例代码
import java.awt.Image;
import java.awt.image.MultiResolutionImage;
// 创建多分辨率图像
MultiResolutionImage mrImage = ...;
List<Image> resolutionVariants = mrImage.getResolutionVariants();
```
## 3. @Deprecated注解增强
@Deprecated注解增加了`since``forRemoval`属性,提供更详细的弃用信息。
```java
// 示例代码在java9以后删除该方法
// since: Java 9 版本
// forRemoval: 是否计划删除该方法
@Deprecated(since = "9", forRemoval = true)
public void oldMethod() {
// 已弃用的方法
}
```
## 4. Stack Walking API
StackWalker 是 Java 9 引入的一个新 API用于高效地遍历和分析当前线程的调用栈。它提供了一种比传统的 Thread.getStackTrace() 和 Throwable.getStackTrace()
更高效、更灵活的方式来访问调用栈信息。
#### StackWalker 的用途
* 性能优化相比旧的栈跟踪方法StackWalker 提供了更好的性能
* 精确控制:可以选择性地过滤和处理栈帧信息
* 安全访问:提供了更安全的栈信息访问方式
* 灵活配置:可以根据需要配置获取不同详细程度的栈信息
#### 主要使用场景
1. 日志记录和调试
```java
// 示例代码
import java.lang.StackWalker;
import java.lang.StackWalker.StackFrame;
public static void main(String[] args) {
StackWalker.getInstance().forEach(frame ->
System.out.println("调用位置: " + frame.getClassName() +
"." + frame.getMethodName() +
"(" + frame.getLineNumber() + ")")
);
}
```
2. 安全检查和权限验证
```java
public class SecurityChecker {
public static boolean isCalledBy(String className) {
return StackWalker.getInstance().walk(stream -> stream.anyMatch(frame -> frame.getClassName().equals(className)));
}
}
```
3. 框架和库开发
```java
// ORM框架可能需要知道哪个类调用了某个方法
public class ORMFramework {
public static void logCaller() {
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
.walk(stream -> {
return stream.skip(1) // 跳过当前方法
.findFirst()
.ifPresent(frame ->
System.out.println("被调用者: " + frame.getDeclaringClass())
);
});
}
}
```
4. 异常处理和上下文分析
```java
public class EnhancedException extends Exception {
public EnhancedException(String message) {
super(message);
this.addContextInfo();
}
private void addContextInfo() {
StackWalker.getInstance().walk(stream -> {
stream.limit(5) // 只获取前5个栈帧
.forEach(frame ->
System.out.println("上下文: " + frame.toString())
);
return null;
});
}
}
```
#### StackWalker 的优势
##### 相比传统方法的优势:
* 性能更好:延迟加载和流式处理,只在需要时获取栈信息
* 内存效率高:不需要创建完整的栈跟踪数组
* 功能更强大:支持过滤、映射等操作
* 类型安全:提供了 StackFrame 接口来表示栈帧信息
##### 主要API
* StackWalker.getInstance() - 获取默认的StackWalker实例
* StackWalker.getInstance(Option...) - 获取带选项的实例
* walker.forEach() - 遍历所有栈帧
* walker.walk(Function) - 使用函数式方法处理栈帧流
## 5. Reactive Streams API (Flow API)
Java 9引入了Flow API支持响应式编程。
```java
// 示例代码
import java.util.concurrent.Flow;
public class MySubscriber implements Flow.Subscriber<String> {
private Flow.Subscription subscription;
@Override
public void onSubscribe(Flow.Subscription subscription) {
this.subscription = subscription;
subscription.request(1);
}
@Override
public void onNext(String item) {
System.out.println("Received: " + item);
subscription.request(1);
}
@Override
public void onError(Throwable throwable) {
System.err.println("Error: " + throwable);
}
@Override
public void onComplete() {
System.out.println("Completed");
}
}
```
## 6. 改进的Javadoc支持HTML5
Java 9的Javadoc工具支持生成HTML5格式的文档并提供搜索功能。
```bash
# 使用示例
javadoc -html5 -d docs src/**/*.java
```
## 7. 统一的JVM日志系统
引入了统一的JVM日志系统使用-Xlog参数进行配置。
```bash
# 示例命令
java -Xlog:gc MyApp
java -Xlog:gc*=debug:gc.log:time,tags MyApp
```
## 8. javac工具的改进
Java 9对编译器进行了多项改进包括更好的诊断信息和性能优化。
```bash
# 示例:使用新的编译器选项
javac --release 9 MyClass.java
```

View File

@@ -0,0 +1,145 @@
## Java 9 主要新特性
---
### 1. 模块系统 (Module System / Jigsaw项目)
1. Java 9引入了模块系统这是Java 9最重要的特性之一。通过[module-info.java](file://F:\coding\java-learning\demo2\java9-example\src\main\java\module-info.java)文件定义模块,可以更好地控制代码的封装和依赖关系。
<br>__示例代码__
```java
// module-info.java
module java9.example {
// 导出包供其他模块使用
exports top.yexuejc.demo;
// 声明需要使用的模块
requires java.base;
}
```
2. 模块声明语法
使用 `module` 关键字声明模块名
使用 `exports` 导出包,使其他模块可以访问
使用 `requires` 声明对其他模块的依赖
3. 模块类型
系统模块Java平台自带的模块如 `java.base`、`java.desktop` 等
应用程序模块:用户自定义的模块
自动模块传统JAR文件在模块路径上的自动模块化表示
未命名模块传统的classpath行为
4. 完整的模块指令
* `exports package`:导出包(公开)
* `exports package to module`:限定导出给特定模块
* `requires module`:依赖模块
* `requires transitive module`:传递依赖
* `uses service`:声明使用的服务
* `provides service with impl`:提供服务实现
* `opens package`:开放包反射访问
* `opens package to module`:限定开放包给特定模块
5. 编译和运行
使用模块系统编译和运行Java程序
```bash
编译
javac --module-path lib -d mods src/main/java/module-info.java src/main/java/top/yexuejc/demo/*.java
# 运行
java --module-path mods -m demo.java.example/top.yexuejc.demo.Main
```
### 2. 接口中的私有方法
Java 9允许在接口中定义私有方法提高代码复用性。
示例代码:
```java
public interface MyInterface {
private void privateMethod() {
System.out.println("Java 9 允许在接口中定义私有方法");
}
default void publicMethod() {
privateMethod(); // 调用私有方法
System.out.println("Java 9 接口私有方法示例");
}
}
```
### 3. 集合工厂方法
Java 9为List、Set和Map接口添加了静态工厂方法`of()`来创建不可变集合。
示例代码:
```java
public static void main(String[] args) {
java.util.List<String> list = java.util.List.of("a", "b", "c");
java.util.Map<String, Integer> map = java.util.Map.of("one", 1, "two", 2);
System.out.println("List: " + list);
System.out.println("Map: " + map);
}
```
### 4. Process API增强
新增了ProcessHandle接口可以更好地管理和控制操作系统进程。
示例代码:
```java
public static void main(String[] args) {
ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("Current Process ID: " + currentProcess.pid());
}
```
### 5. try-with-resources改进
Java 9允许在try-with-resources语句中使用 effectively final 变量。
示例代码:
```java
public static void main(String[] args) {
MyResource resource = new MyResource();
try (resource) {
resource.use();
}
}
```
### 6. JShell (交互式Java REPL)
Java 9引入了交互式Java解释器可以在命令行中直接执行Java代码。
使用方法:
```
bash
jshell
```
## 项目结构
```
src/
├── main/
│ ├── java/
│ │ ├── top/
│ │ │ └── yexuejc/
│ │ │ └── demo/
│ │ │ ├── Main.java
│ │ │ ├── MyInterface.java
│ │ │ └── MyResource.java
│ │ └── module-info.java
```
## 运行项目
确保已安装Java 9或更高版本
```
bash
mvn compile
mvn exec:java -Dexec.mainClass="top.yexuejc.demo.Main"
```
## [其他Java 9特性](README-other.md)
- HTTP/2 Client (孵化阶段)
- Multi-Resolution Images API
- @Deprecated注解增强
- Stack Walking API
- Reactive Streams API (Flow API)
- 改进的Javadoc支持HTML5
- 统一的JVM日志系统
- javac工具的改进
## 参考资料
- [Oracle Java 9 Documentation](https://docs.oracle.com/javase/9/)
- [Java Platform, Standard Edition Documentation](https://docs.oracle.com/javase/9/docs/api/overview-summary.html)
```

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.yexuejc</groupId>
<artifactId>java9-example</artifactId>
<version>1.0.0</version>
<properties>
<java.version>9</java.version>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,8 @@
// Java 9 模块系统示例
module demo.java.example {
// 导出包供其他模块使用
exports top.yexuejc.demo;
// 需要使用的模块
requires java.base;
}

View File

@@ -0,0 +1,38 @@
package top.yexuejc.demo;
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java9.");
// Java 9 新特性示例
// 1. 模块系统 (Module System)
// 请参考 module-info.java 文件
// 2. JShell (交互式Java REPL)
// 可在命令行中使用 jshell 命令体验
// 3. 接口中的私有方法
MyInterface myInterface = new MyInterface() {};
myInterface.publicMethod();
// 4. 集合工厂方法
java.util.List<String> list = java.util.List.of("a", "b", "c");
java.util.Map<String, Integer> map = java.util.Map.of("one", 1, "two", 2);
System.out.println("List: " + list);
System.out.println("Map: " + map);
// 5. Process API增强 新增了ProcessHandle接口可以更好地管理和控制操作系统进程。
ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("Current Process ID: " + currentProcess.pid());
// 6. try-with-resources改进
MyResource resource = new MyResource();
try (resource) {
resource.use();
}
}
}

View File

@@ -0,0 +1,16 @@
package top.yexuejc.demo;
/**
* 接口中的私有方法示例
* @author maxiaofeng
* @date 2025/8/5 17:11
*/
public interface MyInterface {
private void privateMethod() {
System.out.println("Java 9 允许在接口中定义私有方法");
}
default void publicMethod() {
privateMethod(); // 调用私有方法
System.out.println("Java 9 接口私有方法示例");
}
}

View File

@@ -0,0 +1,17 @@
package top.yexuejc.demo;
/**
* try-with-resources改进示例
* @author maxiaofeng
* @date 2025/8/5 17:21
*/
public class MyResource implements AutoCloseable {
public void use() {
System.out.println("使用资源");
throw new RuntimeException("使用资源时出错");
}
@Override
public void close() {
System.out.println("资源已关闭");
}
}

View File

@@ -0,0 +1,32 @@
package top.yexuejc.demo;
import java.lang.StackWalker;
public class StackWalkerDemo {
public static void main(String[] args) {
methodA();
}
public static void methodA() {
methodB();
}
public static void methodB() {
// 使用StackWalker打印调用栈
System.out.println("=== 完整调用栈 ===");
StackWalker.getInstance().forEach(System.out::println);
System.out.println("\n=== 过滤后的调用栈 ===");
// 只显示自定义类的调用栈
StackWalker.getInstance()
.walk(stackFrameStream ->
stackFrameStream
.filter(frame -> frame.getClassName().contains("StackWalkerDemo"))
.peek(frame -> System.out.println(frame.getClassName() + "." + frame.getMethodName()))
);
System.out.println("\n=== 调用者信息 ===");
// 获取直接调用者信息
StackWalker.getInstance().walk(stackStream -> stackStream.skip(1).findFirst()).ifPresent(caller -> System.out.println("调用者: " + caller.getClassName() + "." + caller.getMethodName()));
}
}

View File

@@ -1,98 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>top.yexuejc</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo2</name>
<description>demo2</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<artifactId>java-example</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>java-example</name>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 项目热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 表示依赖不会传递 -->
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork> <!-- 如果没有该配置devtools不会生效 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
<modules>
<module>java8-example</module>
<module>java9-example</module>
<module>java10-example</module>
<module>java11-example</module>
<module>java12-example</module>
<module>java13-example</module>
<module>java14-example</module>
<module>java15-example</module>
<module>java16-example</module>
<module>java17-example</module>
<module>java18-example</module>
<module>java19-example</module>
<module>java20-example</module>
<module>java21-example</module>
<module>java22-example</module>
<module>java23-example</module>
<module>java24-example</module>
</modules>
</project>