提交 4e4177e1 编写于 作者: 小傅哥's avatar 小傅哥

小傅哥,数据库路由组件设计

上级 14e58c1b
......@@ -14,6 +14,7 @@ import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
......@@ -24,6 +25,7 @@ import java.lang.reflect.Method;
* Create by 小傅哥(fustack)
*/
@Aspect
//@Order(1)
@Component("db-router-point")
public class DBRouterJoinPoint {
......@@ -36,12 +38,10 @@ public class DBRouterJoinPoint {
public void aopPoint() {
}
@Around("aopPoint()")
public Object doRouter(ProceedingJoinPoint jp) throws Throwable {
@Around("aopPoint() && @annotation(dbRouter)")
public Object doRouter(ProceedingJoinPoint jp, DBRouter dbRouter) throws Throwable {
Method method = getMethod(jp);
DBRouter dbRouter = method.getAnnotation(DBRouter.class);
// String dbKey = dbRouter.key();
String dbKey = "userId";
String dbKey = dbRouter.key();
if (StringUtils.isBlank(dbKey)) throw new RuntimeException("annotation DBRouter key is null!");
// 计算路由
String dbKeyAttr = getAttrValue(dbKey, jp.getArgs());
......@@ -66,6 +66,10 @@ public class DBRouterJoinPoint {
private Method getMethod(JoinPoint jp) throws NoSuchMethodException {
Signature sig = jp.getSignature();
MethodSignature methodSignature = (MethodSignature) sig;
Class<?>[] interfaces = getClass(jp).getInterfaces();
return getClass(jp).getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
}
......
......@@ -2,13 +2,15 @@ package cn.bugstack.middleware.db.router.annotation;
import java.lang.annotation.*;
/**
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
* 公众号:bugstack虫洞栈
* Create by 小傅哥(fustack)
*/
//@Retention(RetentionPolicy.RUNTIME)
//@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface DBRouter {
String key() default "";
......
package cn.bugstack.middleware.test;
import cn.bugstack.middleware.db.router.annotation.DBRouter;
import org.junit.Test;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;
/**
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
* 公众号:bugstack虫洞栈
......@@ -33,9 +37,22 @@ public class ApiTest {
}
@Test
public void test_str_format(){
public void test_str_format() {
System.out.println(String.format("db%02d", 1));
System.out.println(String.format("_%02d", 25));
}
@Test
public void test_annotation() throws NoSuchMethodException {
Class<IUserDao> iUserDaoClass = IUserDao.class;
Method method = iUserDaoClass.getMethod("insertUser", String.class);
DBRouter dbRouter = method.getAnnotation(DBRouter.class);
System.out.println(dbRouter.key());
}
}
package cn.bugstack.middleware.test;
import cn.bugstack.middleware.db.router.annotation.DBRouter;
/**
* 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
* 公众号:bugstack虫洞栈
* Create by 小傅哥(fustack)
*/
public interface IUserDao {
@DBRouter(key = "userId")
void insertUser(String req);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册