马府强
2 years ago
7 changed files with 314 additions and 0 deletions
@ -0,0 +1,33 @@ |
|||||
|
HELP.md |
||||
|
target/ |
||||
|
!.mvn/wrapper/maven-wrapper.jar |
||||
|
!**/src/main/**/target/ |
||||
|
!**/src/test/**/target/ |
||||
|
|
||||
|
### STS ### |
||||
|
.apt_generated |
||||
|
.classpath |
||||
|
.factorypath |
||||
|
.project |
||||
|
.settings |
||||
|
.springBeans |
||||
|
.sts4-cache |
||||
|
|
||||
|
### IntelliJ IDEA ### |
||||
|
.idea |
||||
|
*.iws |
||||
|
*.iml |
||||
|
*.ipr |
||||
|
|
||||
|
### NetBeans ### |
||||
|
/nbproject/private/ |
||||
|
/nbbuild/ |
||||
|
/dist/ |
||||
|
/nbdist/ |
||||
|
/.nb-gradle/ |
||||
|
build/ |
||||
|
!**/src/main/**/build/ |
||||
|
!**/src/test/**/build/ |
||||
|
|
||||
|
### VS Code ### |
||||
|
.vscode/ |
@ -0,0 +1,11 @@ |
|||||
|
# demo-request-scope |
||||
|
|
||||
|
《基于ThreadLocal实现一个上下文管理组件》技术博客对应源码 |
||||
|
|
||||
|
https://juejin.cn/post/7153287656624324638 |
||||
|
|
||||
|
本文基于`ThreadLocal`原理,实现了一个上下文状态管理组件`Scope`,通过开启一个自定义的`Scope`,在`Scope`范围内,可以通过`Scope`各个方法读写数据; |
||||
|
|
||||
|
通过自定义线程池实现上下文状态数据的线程间传递; |
||||
|
|
||||
|
提出了一种基于`Filter`和`Scope`的`Request`粒度的上下文管理方案。 |
@ -0,0 +1,68 @@ |
|||||
|
<?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"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
<parent> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-parent</artifactId> |
||||
|
<version>2.7.4</version> |
||||
|
<relativePath/> <!-- lookup parent from repository --> |
||||
|
</parent> |
||||
|
<groupId>com.example</groupId> |
||||
|
<artifactId>demo-request-scope</artifactId> |
||||
|
<version>0.0.1-SNAPSHOT</version> |
||||
|
<name>demo-request-scope</name> |
||||
|
<description>Demo project for Spring Boot</description> |
||||
|
<properties> |
||||
|
<java.version>8</java.version> |
||||
|
</properties> |
||||
|
|
||||
|
|
||||
|
<dependencies> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>com.google.guava</groupId> |
||||
|
<artifactId>guava</artifactId> |
||||
|
<version>30.1-jre</version> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-aop</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<!-- 若使用redisson作为分布式锁底层,则需要引入 --> |
||||
|
<dependency> |
||||
|
<groupId>com.baomidou</groupId> |
||||
|
<artifactId>lock4j-redisson-spring-boot-starter</artifactId> |
||||
|
<version>2.2.4</version> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-web</artifactId> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-starter-test</artifactId> |
||||
|
<scope>test</scope> |
||||
|
</dependency> |
||||
|
|
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
<optional>true</optional> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.springframework.boot</groupId> |
||||
|
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
|
</plugin> |
||||
|
</plugins> |
||||
|
</build> |
||||
|
|
||||
|
</project> |
@ -0,0 +1,13 @@ |
|||||
|
package com.example.demo; |
||||
|
|
||||
|
import org.springframework.boot.SpringApplication; |
||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication; |
||||
|
|
||||
|
@SpringBootApplication |
||||
|
public class DemoRequestScopeApplication { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
SpringApplication.run(DemoRequestScopeApplication.class, args); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,167 @@ |
|||||
|
package com.example.demo.service; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Arrays; |
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
//简化你的代码,提高生产力:这10个Lambda表达式必须掌握
|
||||
|
public class TestMain { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
// 1. 使用Lambda表达式进行集合遍历
|
||||
|
// List<String> list = Arrays.asList("apple", "banana", "orange");
|
||||
|
// for (String fruit : list) {
|
||||
|
// System.out.println(fruit);
|
||||
|
// }
|
||||
|
//
|
||||
|
// List<String> list2 = Arrays.asList("apple", "banana", "orange");
|
||||
|
// list2.forEach(fruit -> System.out.println(fruit));
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// 2. 使用Lambda表达式进行排序
|
||||
|
// List<String> list2 = Arrays.asList("apple", "banana", "orange");
|
||||
|
// Collections.sort(list2, new Comparator<String>() {
|
||||
|
// public int compare(String s1, String s2) {
|
||||
|
// return s1.compareTo(s2);
|
||||
|
// }
|
||||
|
// });
|
||||
|
//
|
||||
|
// List<String> list = Arrays.asList("apple", "banana", "orange");
|
||||
|
// Collections.sort(list, (s1, s2) -> s1.compareTo(s2));
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// 3. 使用Lambda表达式进行过滤
|
||||
|
// List<String> list = Arrays.asList("apple", "banana", "orange");
|
||||
|
// List<String> filteredList = new ArrayList<String>();
|
||||
|
// for (String fruit : list) {
|
||||
|
// if (fruit.startsWith("a")) {
|
||||
|
// filteredList.add(fruit);
|
||||
|
// }
|
||||
|
// }
|
||||
|
//
|
||||
|
// List<String> list2 = Arrays.asList("apple", "banana", "orange");
|
||||
|
// List<String> filteredLists = list2.
|
||||
|
// stream().
|
||||
|
// filter(fruit -> fruit.startsWith("a")).
|
||||
|
// collect(Collectors.toList());
|
||||
|
|
||||
|
|
||||
|
// 4. 使用Lambda表达式进行映射
|
||||
|
// List<String> list = Arrays.asList("apple", "banana", "orange");
|
||||
|
// List<Integer> lengths = new ArrayList<Integer>();
|
||||
|
// for (String fruit : list) {
|
||||
|
// lengths.add(fruit.length());
|
||||
|
// }
|
||||
|
//
|
||||
|
// List<String> lists = Arrays.asList("apple", "banana", "orange");
|
||||
|
// List<Integer> lengthss = lists.stream().map(fruit -> fruit.length())
|
||||
|
// .collect(Collectors.toList());
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// 5. 使用Lambda表达式进行归约
|
||||
|
// List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
|
||||
|
// int sum = 0;
|
||||
|
// for (int i : list) {
|
||||
|
// sum += i;
|
||||
|
// }
|
||||
|
//
|
||||
|
// List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5);
|
||||
|
// int sum2 = list2.stream().reduce(0, (a, b) -> a + b);
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// 6. 使用Lambda表达式进行分组
|
||||
|
// List<String> list = Arrays.asList("apple", "banana", "orange");
|
||||
|
// Map<Integer, List<String>> grouped = new HashMap<Integer, List<String>>();
|
||||
|
// for (String fruit : list) {
|
||||
|
// int length = fruit.length();
|
||||
|
// if (!grouped.containsKey(length)) {
|
||||
|
// grouped.put(length, new ArrayList<String>());
|
||||
|
// }
|
||||
|
// grouped.get(length).add(fruit);
|
||||
|
// }
|
||||
|
//
|
||||
|
//
|
||||
|
// List<String> lists = Arrays.asList("apple", "banana", "orange");
|
||||
|
// Map<Integer, List<String>> groupeds = lists.stream().collect(Collectors.groupingBy(fruit -> fruit.length()));
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// 7. 使用Lambda表达式进行函数式接口的实现
|
||||
|
// MyInterface myObject = new MyInterface() {
|
||||
|
// public void doSomething(String input) {
|
||||
|
// System.out.println(input);
|
||||
|
// }
|
||||
|
// };
|
||||
|
// myObject.doSomething("Hello World");
|
||||
|
//
|
||||
|
// MyInterface myObject = input -> System.out.println(input);
|
||||
|
// myObject.doSomething("Hello World");
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
//8. 使用Lambda表达式进行线程的创建
|
||||
|
// Thread thread = new Thread(new Runnable() {
|
||||
|
// public void run() {
|
||||
|
// System.out.println("Thread is running.");
|
||||
|
// }
|
||||
|
// });
|
||||
|
// thread.start();
|
||||
|
//
|
||||
|
// Thread threads = new Thread(() -> System.out.println("Thread is running."));
|
||||
|
// threads.start();
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// 9. 使用Lambda表达式进行Optional的操作
|
||||
|
// String str = "Hello World";
|
||||
|
// if (str != null) {
|
||||
|
// System.out.println(str.toUpperCase());
|
||||
|
// }
|
||||
|
//
|
||||
|
// Optional<String> strs = Optional.ofNullable("Hello World");
|
||||
|
// strs.map(String::toUpperCase).ifPresent(System.out::println);
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
// 10. 使用Lambda表达式进行Stream的流水线操作
|
||||
|
|
||||
|
List<String> list = Arrays.asList("apple", "banana", "orange"); |
||||
|
List<String> filteredList = new ArrayList<String>(); |
||||
|
for (String fruit : list) { |
||||
|
if (fruit.startsWith("a")) { |
||||
|
filteredList.add(fruit.toUpperCase()); |
||||
|
} |
||||
|
} |
||||
|
Collections.sort(filteredList); |
||||
|
|
||||
|
List<String> lists = Arrays.asList("apple", "banana", "orange"); |
||||
|
List<String> filteredLists = lists.stream().filter(fruit -> fruit.startsWith("a")).map(String::toUpperCase).sorted().collect(Collectors.toList()); |
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1 @@ |
|||||
|
|
@ -0,0 +1,21 @@ |
|||||
|
spring: |
||||
|
redis: |
||||
|
database: 0 |
||||
|
# Redis服务器地址 写你的ip |
||||
|
host: 192.168.56.10 |
||||
|
# Redis服务器连接端口 |
||||
|
port: 6379 |
||||
|
# Redis服务器连接密码(默认为空) |
||||
|
password: 123456 |
||||
|
# 连接池最大连接数(使用负值表示没有限制 类似于mysql的连接池 |
||||
|
jedis: |
||||
|
pool: |
||||
|
max-active: 200 |
||||
|
# 连接池最大阻塞等待时间(使用负值表示没有限制) 表示连接池的链接拿完了 现在去申请需要等待的时间 |
||||
|
max-wait: -1 |
||||
|
# 连接池中的最大空闲连接 |
||||
|
max-idle: 10 |
||||
|
# 连接池中的最小空闲连接 |
||||
|
min-idle: 0 |
||||
|
# 连接超时时间(毫秒) 去链接redis服务端 |
||||
|
timeout: 6000 |
Loading…
Reference in new issue