Skip to content

Commit 4a69045

Browse files
committed
本例主要演示如何通过aop解析spel表达式实现一个不一样的切面
1 parent 250e44f commit 4a69045

File tree

16 files changed

+505
-0
lines changed

16 files changed

+505
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,5 @@
660660
/springboot-mybatisplus-tenant/target/classes/META-INF/
661661
/springboot-xxl-job-executor/target/classes/
662662
/springboot-xxl-job-executor/springboot-xxl-job-executor.iml
663+
/springboot-aop-spel/springboot-aop-spel.iml
664+
/springboot-aop-spel/target/classes/

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<module>springboot-generate-sqlscript</module>
3838
<module>springboot-mybatisplus-tenant</module>
3939
<module>springboot-xxl-job-executor</module>
40+
<module>springboot-aop-spel</module>
4041
</modules>
4142
<parent>
4243
<groupId>org.springframework.boot</groupId>

springboot-aop-spel/pom.xml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springboot-learning</artifactId>
7+
<groupId>com.github.lybgeek</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>springboot-aop-spel</artifactId>
13+
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-cache</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-aop</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-configuration-processor</artifactId>
32+
<optional>true</optional>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.projectlombok</groupId>
36+
<artifactId>lombok</artifactId>
37+
<optional>true</optional>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>cn.hutool</groupId>
46+
<artifactId>hutool-all</artifactId>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
<configuration>
56+
<excludes>
57+
<exclude>
58+
<groupId>org.projectlombok</groupId>
59+
<artifactId>lombok</artifactId>
60+
</exclude>
61+
</excludes>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
67+
68+
69+
</project>

springboot-aop-spel/readMe.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
本文通过自定义注解+AOP+SPEL实现一个自定义缓存切面
2+
3+
亮点在于利用aop加SpelExpressionParser解析器解析spel实现更灵活的切面
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.lybgeek;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
7+
@SpringBootApplication
8+
@EnableScheduling
9+
public class SpringbootAopSpelApplication{
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringbootAopSpelApplication.class, args);
13+
}
14+
15+
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.lybgeek.cache;
2+
3+
4+
import java.util.Map;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
7+
public enum LocalCache {
8+
9+
INSTANCE;
10+
11+
private Map<String,Object> localCacheMap = new ConcurrentHashMap<>();
12+
13+
public void put(String key,Object obj){
14+
localCacheMap.put(key,obj);
15+
}
16+
17+
public Object get(String key){
18+
return localCacheMap.get(key);
19+
}
20+
21+
public void printCacheInfo(){
22+
System.out.println(String.format("cacheInfo:%s",localCacheMap));
23+
24+
}
25+
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.lybgeek.cache.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
@Target({ElementType.TYPE, ElementType.METHOD})
6+
@Retention(RetentionPolicy.RUNTIME)
7+
@Inherited
8+
@Documented
9+
public @interface LocalCacheable {
10+
String key() default "";
11+
String condition() default "";
12+
13+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.lybgeek.cache.aspect;
2+
3+
4+
import com.github.lybgeek.cache.LocalCache;
5+
import com.github.lybgeek.cache.annotation.LocalCacheable;
6+
import com.github.lybgeek.util.SpELParserUtils;
7+
import org.aspectj.lang.ProceedingJoinPoint;
8+
import org.aspectj.lang.annotation.Around;
9+
import org.aspectj.lang.annotation.Aspect;
10+
import org.aspectj.lang.reflect.MethodSignature;
11+
import org.springframework.stereotype.Component;
12+
import org.springframework.util.StringUtils;
13+
14+
import java.lang.reflect.Method;
15+
16+
@Component
17+
@Aspect
18+
public class CacheAspect {
19+
20+
21+
@Around("@annotation(localCacheable)")
22+
public Object around(ProceedingJoinPoint pjp, LocalCacheable localCacheable) throws Throwable{
23+
MethodSignature methodSignature = (MethodSignature)pjp.getSignature();
24+
Method method = methodSignature.getMethod();
25+
Object[] args = pjp.getArgs();
26+
Object result = pjp.proceed();
27+
String key = pjp.getTarget().getClass().getName() + "_" + method.getName() + "_" + args.length;
28+
29+
if(!StringUtils.isEmpty(localCacheable.key())){
30+
key = SpELParserUtils.parse(method,args,localCacheable.key(),String.class);
31+
}
32+
33+
System.out.println("key:"+key);
34+
35+
if(!StringUtils.isEmpty(localCacheable.condition())){
36+
boolean condition = SpELParserUtils.parse(method,args,localCacheable.condition(),Boolean.class);
37+
if(condition){
38+
LocalCache.INSTANCE.put(key,result);
39+
}
40+
}else{
41+
LocalCache.INSTANCE.put(key,result);
42+
}
43+
44+
return result;
45+
46+
}
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.lybgeek.cache.event;
2+
3+
4+
import com.github.lybgeek.cache.LocalCache;
5+
import org.springframework.context.ApplicationListener;
6+
import org.springframework.context.event.ContextRefreshedEvent;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.ScheduledExecutorService;
11+
import java.util.concurrent.TimeUnit;
12+
import java.util.concurrent.atomic.AtomicInteger;
13+
14+
@Component
15+
public class CacheContextEvent implements ApplicationListener<ContextRefreshedEvent> {
16+
17+
private AtomicInteger atomicInteger = new AtomicInteger();
18+
19+
private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10, runnable -> {
20+
Thread thread = new Thread(runnable);
21+
thread.setName("cache-pool-"+atomicInteger.getAndIncrement());
22+
return thread;
23+
});
24+
25+
@Override
26+
public void onApplicationEvent(ContextRefreshedEvent event) {
27+
executorService.scheduleWithFixedDelay((Runnable) () -> {
28+
LocalCache.INSTANCE.printCacheInfo();
29+
},5,5, TimeUnit.SECONDS);
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.lybgeek.dao;
2+
3+
4+
import com.github.lybgeek.entity.Product;
5+
import org.springframework.stereotype.Repository;
6+
7+
import java.util.Map;
8+
import java.util.Objects;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
import java.util.concurrent.atomic.LongAdder;
11+
12+
@Repository
13+
public class ProductMockDao {
14+
private LongAdder idIncrService = new LongAdder();
15+
16+
private static Map<Long, Product> mockDb = new ConcurrentHashMap<>();
17+
18+
public Product save(Product product) {
19+
if(Objects.isNull(product.getId()) || !mockDb.containsKey(product.getId())){
20+
idIncrService.increment();
21+
product.setId(idIncrService.longValue());
22+
}
23+
mockDb.put(product.getId(),product);
24+
25+
return product;
26+
}
27+
}

0 commit comments

Comments
 (0)