Skip to content

Commit

Permalink
2025-01-04 18:41:51
Browse files Browse the repository at this point in the history
  • Loading branch information
yingzhuo committed Jan 4, 2025
1 parent d7ca043 commit f204f29
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface DataSourceSwitch {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,44 @@
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import spring.turbo.core.AspectUtils;

/**
* @author 应卓
* @see DataSourceSwitch
* @since 3.4.1
*/
@Aspect
public record DataSourceSwitchAdvice(int order) implements Ordered {
public class DataSourceSwitchAdvice implements Ordered {

private final int order;

public DataSourceSwitchAdvice() {
this(HIGHEST_PRECEDENCE);
}

public DataSourceSwitchAdvice(int order) {
this.order = order;
}

@Around("@annotation(spring.turbo.module.jdbc.ds.DataSourceSwitch)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
var signature = joinPoint.getSignature();
if (signature instanceof MethodSignature methodSignature) {
var annotation = methodSignature.getMethod().getAnnotation(DataSourceSwitch.class);
var dataSourceName = annotation.value();
RoutingDataSourceLookup.set(dataSourceName);
try {
return joinPoint.proceed();
} finally {
RoutingDataSourceLookup.remove();
}
} else {
var annotation = AspectUtils.getMethodAnnotation(joinPoint, DataSourceSwitch.class);

if (annotation == null) {
annotation = AspectUtils.getObjectTypeAnnotation(joinPoint, DataSourceSwitch.class);
}

if (annotation == null) {
return joinPoint.proceed();
}

try {
RoutingDataSourceLookup.set(annotation.value());
return joinPoint.proceed();
} finally {
RoutingDataSourceLookup.remove();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface AvoidRepeatedInvocation {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ public AvoidRepeatedInvocationAdvice(RedisOperations<String, String> redisOperat
@Around("@annotation(spring.turbo.module.redis.aspect.AvoidRepeatedInvocation)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
var annotation = AspectUtils.getMethodAnnotation(joinPoint, AvoidRepeatedInvocation.class);

if (annotation == null) {
annotation = AspectUtils.getObjectTypeAnnotation(joinPoint, AvoidRepeatedInvocation.class);
}

if (annotation == null) {
return joinPoint.proceed();
}

var redisKey = AspectSpELTemplate.<String>newInstance(annotation.value(), joinPoint)
var redisKey = AspectSpELTemplate.newInstance(annotation.value(), joinPoint)
.setRootObject(null)
.getValue();

Expand Down

0 comments on commit f204f29

Please sign in to comment.