-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Spring AOP | ||
|
||
### 简介 | ||
|
||
AOP(Aspect-Oriented Programming,面向切面编程)是一种编程范式,旨在将应用程序中的各种功能分离出来,以提高代码的可重用性和可维护性。AOP 通过将交叉业务逻辑封装到切面中,可以在不修改原有代码的情况下对其进行扩展和维护。 | ||
|
||
### AOP 的存在价值 | ||
|
||
在传统 OOP 编程里以对象为核心,整个软件系统由系列相互依赖的对象所组成,而这些对象将被抽象成一个一个的类,并允许使用类继承来管理类与类之间一般到特殊的关系。随着软件规模的增大,应用的逐渐升级,慢慢出现了一些 OOP 很难解决的问题。 | ||
|
||
- **代码复用性差**:由于面向对象的设计思想,导致代码复用性差。比如,在两个类中,有很多相似的代码,如果使用 OOP 思想,需要将相似的代码复制到两个类中,这会导致代码的重复性很高。 | ||
- **代码可维护性差**:由于 OOP 思想将代码封装到类中,导致代码的可维护性差。如果需要修改某个功能,需要修改整个类的代码,这会导致代码的维护性很低。 | ||
- **代码可扩展性差**:由于 OOP 思想将代码封装到类中,导致代码的可扩展性差。如果需要添加一个新的功能,需要修改整个类的代码,这会导致代码的扩展性很低。 | ||
|
||
AOP 就是为了解决这些问题而出现的。AOP 可以将交叉业务逻辑封装到切面中,在运行时将切面与业务逻辑编织在一起,从而实现代码的复用、可维护性和可扩展性。 | ||
|
||
### AOP 的分类 | ||
|
||
AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,AOP 代理主要分为静态代理和动态代理两大类,静态代理以 AspectJ 为代表;而动态代理则以 Spring AOP 为代表。 | ||
|
||
其中静态代理是指使用 AOP 框架提供的命令进行编译,从而在编译阶段就可生成 AOP 代理类,因此也称为编译时增强;而动态代理则在运行时借助于 JDK 动态代理、CGLIB 等在内存中“临时”生成 AOP 动态代理类,因此也被称为运行时增强。 | ||
|
||
Spring AOP 既支持静态代理也支持动态代理。 | ||
|
||
### 总结 | ||
|
||
AOP 广泛应用于处理一些具有横切性质的系统级服务,AOP 的出现是对 OOP 的良好补充,它使得开发者能用更优雅的方式处理具有横切性质的服务。不管是那种 AOP 实现,不论是 AspectJ、还是 Spring AOP,它们都需要动态地生成一个 AOP 代理类,区别只是生成 AOP 代理类的时机不同:AspectJ 采用编译时生成 AOP 代理类,因此具有更好的性能,但需要使用特定的编译器进行处理;而 Spring AOP 则采用运行时生成 AOP 代理类,因此无需使用特定编译器进行处理。由于 Spring AOP 需要在每次运行时生成 AOP 代理,因此性能略差一些。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# JDK17 | ||
|
||
### 限制类继承 sealed 类 | ||
|
||
Sealed 类是一种新的类修饰符,用于限制类的继承。Sealed 类可以控制哪些类可以继承自它,这样可以使得代码更加安全、可维护。Sealed 类的使用可以在编译时强制执行一些规则,从而避免运行时错误。 | ||
|
||
### Pattern Matching for Switch 语法 | ||
|
||
```Java | ||
public static void main(String[] args) { | ||
Object obj = "hello"; | ||
|
||
switch (obj) { | ||
case String s && s.length() > 5 -> System.out.println("长字符串"); | ||
case String s -> System.out.println("短字符串"); | ||
case Integer i -> System.out.println("整型数"); | ||
default -> System.out.println("不支持的类型"); | ||
} | ||
} | ||
``` | ||
|
||
### Record 类 | ||
|
||
Record 类是一种新的数据类,可以用于定义只有属性和访问器的简单数据对象。Record 类可以简化代码,使得代码更加易读、易维护。Record 类的使用可以减少代码量,避免出现大量的 getter 和 setter 方法。 | ||
|
||
```Java | ||
public record Person(String name, int age) {} | ||
|
||
public class RecordExample { | ||
public static void main(String[] args) { | ||
Person person = new Person("John", 30); | ||
|
||
System.out.println("Name: " + person.name()); | ||
System.out.println("Age: " + person.age()); | ||
} | ||
} | ||
``` | ||
|
||
在这个示例中,我们定义了一个名为 Person 的 Record 类,它有两个字段:name 和 age。Record 类会自动生成一个带有这些字段的构造函数、getter 方法和 equals、hashCode 和 toString 方法。 | ||
|
||
### 增强的 Java 集合库 | ||
|
||
#### of() 方法:创建一个不可变的集合 | ||
|
||
```Java | ||
List<String> list = List.of("apple", "banana", "orange"); | ||
Set<Integer> set = Set.of(1, 2, 3, 4); | ||
Map<String, Integer> map = Map.of("apple", 1, "banana", 2, "orange", 3); | ||
``` | ||
|
||
#### forEach() 方法:遍历集合 | ||
|
||
```Java | ||
List<String> list = List.of("apple", "banana", "orange"); | ||
list.forEach(name -> System.out.println(name)); | ||
Set<Integer> set = Set.of(1, 2, 3, 4); | ||
set.forEach(number -> System.out.println(number)); | ||
``` | ||
|
||
#### Collectors 类:提供了一系列的归约操作 | ||
|
||
```Java | ||
List<String> list = List.of("apple", "banana", "orange"); | ||
String joinedString = list.stream().collect(Collectors.joining("-", "[", "]")); | ||
System.out.println(joinedString); | ||
|
||
Map<String, Integer> map = Map.of("apple", 1, "banana", 2, "orange", 3); | ||
Map<Integer, String> reversedMap = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); | ||
System.out.println(reversedMap); | ||
``` | ||
|
||
#### takeWhile() 方法和 dropWhile() 方法:根据条件截取集合 | ||
|
||
```Java | ||
List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7); | ||
List<Integer> takenList = list.stream().takeWhile(number -> number < 5).collect(Collectors.toList()); | ||
System.out.println(takenList); | ||
|
||
List<Integer> dropedList = list.stream().dropWhile(number -> number < 5).collect(Collectors.toList()); | ||
System.out.println(dropedList); | ||
``` | ||
|
||
#### toArray(IntFunction<T[]>) 方法:返回集合中的所有元素到一个新数组中 | ||
|
||
```Java | ||
List<String> list = List.of("apple", "banana", "orange"); | ||
String[] array = list.toArray(String[]::new); | ||
System.out.println(Arrays.toString(array)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#### 虚拟线程 | ||
|
||
- 虚拟线程是轻量级的,它们与内核线程无关,因此应用程序可以具有比具有多线程的线程更少的核心。 | ||
- 虚拟线程在 JDK 19 中引入,JDK21 正式发布,旨在提高可伸缩性和效率。 | ||
- 虚拟线程不是用来提速的,而是用来提高吞吐量的。 | ||
|
||
#### 参考 | ||
|
||
- [「后端」看完便知 JDK21 中虚拟线程到底是什么!](https://zhuanlan.zhihu.com/p/659965075) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
--- | ||
sidebar_position: 1 | ||
sidebar_position: 2 | ||
--- | ||
|
||
# 概述 | ||
# Java | ||
|
||
一种使用非常广泛的跨平台编程语言。 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Kotlin | ||
|
||
Kotlin(科特林)是一个用于现代多平台应用的静态编程语言,由 JetBrains 开发。 | ||
|
||
Kotlin 可以编译成 Java 字节码,也可以编译成 JavaScript,方便在没有 JVM 的设备上运行。除此之外 Kotlin 还可以编译成二进制代码直接运行在机器上(例如嵌入式设备或 iOS)。 | ||
|
||
Kotlin 已正式成为 Android 官方支持开发语言。 | ||
|
||
[官方网站](https://kotlinlang.org/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Lua | ||
|
||
### 学习资料 | ||
|
||
- [Lua 笔记系列](https://www.junmajinlong.com/lua/index/) |