使用aop、类反射机制、动态代理截获jpa标签@query内容实现适配器(实例)

举例说明:

假如我们想在原有的spring data jpa进行数据持久化的系统上,不改变任何原有代码来实现打印出我们每次操作执行的sql语句(nativeQuery),要怎么来实现呢?

或许可以在每个语句前加入一个新的自定义注解,设置其内容,实现一个日志的功能,但如果在不更改原有代码又极简代码的情况下,我想到的方案是利用aop和类反射机制直接获取@Query的value,再进行操作。

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.data.jpa.repository.Query;
import java.util.Date;

@Order(1)
@Component
@Aspect
public class QueryAspect {

    @Around("@annotation(org.springframework.data.jpa.repository.Query)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("QueryAspect--around()启动" + new Date());

        Query query = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(Query.class);
        String sql = query.value();

        System.out.println(sql);

        return joinPoint.proceed();
    }

}

 

由此我们可以看到,执行的语句成功打印,得到这个输出后我们就可以进行很多操作,比如把这个操作过程存到日志或者某个记录操作的表里,再比如想对操作次数或者某些统计需要时以此实现 。

在到这里之后我们再继续深入,假如我们想在得到这个语句后,再进行一次加工,让其转变格式,并返回给jpa继续执行,这样便实现了一款强大的适配器,可以在一个项目改变底层数据库所支持的sql语句格式不同时实现无需更改便可以适配的强大功能,仅仅以这个思路举一个简单例子,大家可以拓宽思路,实现自己独特的功能。

在原有基础上,使用InvocationHandler动态代理更改内存里的sql的值,以神通数据库为例,神通数据库每个表名前需要加上模式名,以下代码以此为例。

package com.adapter.demo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.data.jpa.repository.Query;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

@Order(1)
@Component
@Aspect
public class QueryAspect {

    @Around("@annotation(org.springframework.data.jpa.repository.Query)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{

        Query query = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(Query.class);

        InvocationHandler h = Proxy.getInvocationHandler(query);

        String sql = query.value();
        String[] sqls = sql.split(" ");
        String model = "DATAASSETMNT.";

        for(int i = 1 ; i < sqls.length && !sql.contains(model);i++){
            if(sqls[i-1].equals("from") || sqls[i-1].equals("update") || sqls[i-1].equals("delete") || sqls[i-1].equals("union") ){
               sqls[i]=model+sqls[i];
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String s: sqls
             ) {
            stringBuilder.append(s);
            stringBuilder.append(' ');
        }
        System.out.println(stringBuilder);

        Field hField = h.getClass().getDeclaredField("memberValues");
        hField.setAccessible(true);
        Map memberValues = (Map) hField.get(h);

        memberValues.put("value",stringBuilder.toString());
        String value =  query.value();
        System.out.println("===="+value);

        return joinPoint.proceed();
    }

}

博客交流群:416424884

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值