File tree Expand file tree Collapse file tree 4 files changed +47
-2
lines changed
src/main/java/com/hand/springbootdataaccess Expand file tree Collapse file tree 4 files changed +47
-2
lines changed Original file line number Diff line number Diff line change 50
50
<groupId >org.springframework.boot</groupId >
51
51
<artifactId >spring-boot-starter-web</artifactId >
52
52
</dependency >
53
+ <dependency >
54
+ <groupId >org.springframework.boot</groupId >
55
+ <artifactId >spring-boot-starter-aop</artifactId >
56
+ </dependency >
53
57
</dependencies >
54
58
55
59
<build >
Original file line number Diff line number Diff line change
1
+ package com .hand .springbootdataaccess .aop ;
2
+
3
+ import com .hand .springbootdataaccess .config .RoutingDataSourceContext ;
4
+ import com .hand .springbootdataaccess .config .RoutingWith ;
5
+ import org .aspectj .lang .ProceedingJoinPoint ;
6
+ import org .aspectj .lang .annotation .Around ;
7
+ import org .aspectj .lang .annotation .Aspect ;
8
+ import org .springframework .stereotype .Component ;
9
+
10
+ /**
11
+ * @author zijian.zeng@hand-china.com
12
+ * @since 2023-01-30
13
+ */
14
+ @ Aspect
15
+ @ Component
16
+ public class RoutingAspect {
17
+
18
+ @ Around ("@annotation(routingWith)" )
19
+ public Object routingWithDataSource (ProceedingJoinPoint joinPoint , RoutingWith routingWith ) throws Throwable {
20
+ new RoutingDataSourceContext (routingWith .key ());
21
+ return joinPoint .proceed ();
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package com .hand .springbootdataaccess .config ;
2
+
3
+ import java .lang .annotation .ElementType ;
4
+ import java .lang .annotation .Retention ;
5
+ import java .lang .annotation .RetentionPolicy ;
6
+ import java .lang .annotation .Target ;
7
+
8
+ /**
9
+ * @author zijian.zeng@hand-china.com
10
+ * @since 2023-01-30
11
+ */
12
+ @ Target (ElementType .METHOD )
13
+ @ Retention (RetentionPolicy .RUNTIME )
14
+ public @interface RoutingWith {
15
+
16
+ String key () default "master" ;
17
+ }
Original file line number Diff line number Diff line change 2
2
3
3
import com .hand .springbootdataaccess .config .MyDataSourceAutoConfiguration ;
4
4
import com .hand .springbootdataaccess .config .RoutingDataSourceContext ;
5
+ import com .hand .springbootdataaccess .config .RoutingWith ;
5
6
import com .hand .springbootdataaccess .service .ProductService ;
6
7
import org .springframework .beans .factory .annotation .Autowired ;
7
8
import org .springframework .web .bind .annotation .RequestMapping ;
@@ -19,15 +20,15 @@ public class ProductController {
19
20
20
21
21
22
@ RequestMapping ("/findAllM" )
23
+ @ RoutingWith
22
24
public String findAllM () {
23
- new RoutingDataSourceContext (MyDataSourceAutoConfiguration .MASTER );
24
25
productService .findAllProductM ();
25
26
return "m" ;
26
27
}
27
28
28
29
@ RequestMapping ("/findAllS" )
30
+ @ RoutingWith (key = "slave" )
29
31
public String findAllS () {
30
- new RoutingDataSourceContext (MyDataSourceAutoConfiguration .SLAVE );
31
32
productService .findAllProductS ();
32
33
return "s" ;
33
34
}
You can’t perform that action at this time.
0 commit comments