aop 切点表达式
Spring AOP切点表达式-SpringBoot从入门到熟悉(十五)
修改dao层所有方法的返回值
问题场景
- 项目数据源需要从Oracle转为mysql,因此要对mybatis mapper文件做修改
- 但是mybatis 文件里写的sql语句有很多返回的数据库字段是小写英文,在oracle返回结果字段都是大写,而mysql返回字段包含小写英文,因此在使用mysql数据源的情况下,java代码获取的是大写英文字段,就会获取不到指定字段。
mapper文件: select station_name,bay_name from stationTable; java代码: String stationName=stationBayList.get(i).get(“STATION_NAME”).toString(); (切换成mysql数据源会报错,因为返回的key 是 “station_name”)
aop解决方案
- 切点为将dao层返回类型为 List(Map) 的所有函数
- 实现将 List(Map) 或者 Map 中的key 全部转为大写
实现要点
获取函数返回值,判断返回类型为List 或者 Map
获取函数返回类型,用 instanceof 关键字判断
object=proceedingJoinPoint.proceed();
if(object instanceof List){
List objectList=(List)object;
if(objectList.size() > 0 && objectList.get(0) instanceof Map){
List<Map<String,Object>> recordList=(ArrayList<Map<String,Object>>)object;
recordList=changeKeyToUpperCaseList(recordList);
return recordList;
}
}else if(object instanceof Map){
Map<String,Object> record=(Map<String,Object>)object;
record=changeKeyToUpperCase(record);
return record;
}
concurrentModifyException Map的并发修改异常
转为 ConcurrentHashMap
records=records.stream().map(map -> {
ConcurrentHashMap<String,Object> concurrentHashMap=new ConcurrentHashMap(map);
Map<String,Object> newMap=new HashMap<>();
for(String key:concurrentHashMap.keySet()){
newMap.put(key.toUpperCase(),concurrentHashMap.get(key));
}
return newMap;
}).collect(Collectors.toList());
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@Aspect
@Component
public class DaoAspect {
@Pointcut("execution(* com.dfe.e8800p.scheduler.dao.*.*(..))")
public void daoPoint() {
}
@Around(value = "daoPoint()")
public Object aroundSystemDeficiency(ProceedingJoinPoint proceedingJoinPoint){
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = signature.getMethod();
Class returnType= method.getReturnType();
Object object = null;
try {
object=proceedingJoinPoint.proceed();
if(object instanceof List){
List objectList=(List)object;
if(objectList.size() > 0 && objectList.get(0) instanceof Map){
List<Map<String,Object>> recordList=(ArrayList<Map<String,Object>>)object;
recordList=changeKeyToUpperCaseList(recordList);
return recordList;
}
}else if(object instanceof Map){
Map<String,Object> record=(Map<String,Object>)object;
record=changeKeyToUpperCase(record);
return record;
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return object;
}
public List<Map<String,Object>> changeKeyToUpperCaseList(List<Map<String,Object>> records){
records=records.stream().map(map -> {
ConcurrentHashMap<String,Object> concurrentHashMap=new ConcurrentHashMap(map);
Map<String,Object> newMap=new HashMap<>();
for(String key:concurrentHashMap.keySet()){
newMap.put(key.toUpperCase(),concurrentHashMap.get(key));
}
return newMap;
}).collect(Collectors.toList());
return records;
}
public Map<String,Object> changeKeyToUpperCase(Map<String,Object> record){
ConcurrentHashMap<String,Object> concurrentHashMap=new ConcurrentHashMap(record);
Map<String,Object> newMap=new HashMap<>();
for(String key:concurrentHashMap.keySet()){
newMap.put(key.toUpperCase(),concurrentHashMap.get(key));
}
return newMap;
}
}
|