Skip to content

Commit 4462865

Browse files
author
曾梓健
committed
动态数据源实现[优化]: 使用aop加注解 优化代码
1 parent 2ec6726 commit 4462865

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

springboot-dataaccess/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<groupId>org.springframework.boot</groupId>
5151
<artifactId>spring-boot-starter-web</artifactId>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-aop</artifactId>
56+
</dependency>
5357
</dependencies>
5458

5559
<build>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

springboot-dataaccess/src/main/java/com/hand/springbootdataaccess/controller/ProductController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.hand.springbootdataaccess.config.MyDataSourceAutoConfiguration;
44
import com.hand.springbootdataaccess.config.RoutingDataSourceContext;
5+
import com.hand.springbootdataaccess.config.RoutingWith;
56
import com.hand.springbootdataaccess.service.ProductService;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.web.bind.annotation.RequestMapping;
@@ -19,15 +20,15 @@ public class ProductController {
1920

2021

2122
@RequestMapping("/findAllM")
23+
@RoutingWith
2224
public String findAllM() {
23-
new RoutingDataSourceContext(MyDataSourceAutoConfiguration.MASTER);
2425
productService.findAllProductM();
2526
return "m";
2627
}
2728

2829
@RequestMapping("/findAllS")
30+
@RoutingWith(key = "slave")
2931
public String findAllS() {
30-
new RoutingDataSourceContext(MyDataSourceAutoConfiguration.SLAVE);
3132
productService.findAllProductS();
3233
return "s";
3334
}

0 commit comments

Comments
 (0)