package com.yihu.hos.interceptor; import com.yihu.hos.common.constants.ContextAttributes; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; /** * jdbc * @author HZY * @vsrsion 1.0 * Created at 2016/12/7. */ @Aspect @Component public class JdbcTemplateAdvice { @Around("execution(* org.springframework.jdbc.core.JdbcTemplate.query*(..)) || execution(* org.springframework.jdbc.core.JdbcTemplate.execute*(..))") public Object process(ProceedingJoinPoint point) throws Throwable { String schemaName = getSchema(); //访问目标方法的参数: Object[] args = point.getArgs(); if (args != null && args.length > 0 && args[0].getClass() == String.class) { String completeSql = args[0].toString(); if (StringUtils.isNotEmpty(schemaName)) { String myCatAnnotation = "/*#mycat:schema=" + schemaName + "*/ "; completeSql = myCatAnnotation + completeSql; } args[0]=completeSql; } //用改变后的参数执行目标方法 Object returnValue = point.proceed(args); return returnValue; } // @Before("execution(* org.springframework.jdbc.core.JdbcTemplate.query*(..)) || execution(* org.springframework.jdbc.core.JdbcTemplate.execute*(..))") // public void permissionCheck(JoinPoint point) { // System.out.println("@Before:模拟权限检查..."); // System.out.println("@Before:目标方法为:" + // point.getSignature().getDeclaringTypeName() + // "." + point.getSignature().getName()); // System.out.println("@Before:参数为:" + Arrays.toString(point.getArgs())); // System.out.println("@Before:被织入的目标对象为:" + point.getTarget()); // // String schemaName = getSchema(); // //访问目标方法的参数: // Object[] args = point.getArgs(); // if (args != null && args.length > 0 && args[0].getClass() == String.class) { // String completeSql = args[0].toString(); // if (StringUtils.isNotEmpty(schemaName)) { // String myCatAnnotation = "/*#mycat:schema=" + schemaName + "*/ "; // completeSql = myCatAnnotation + completeSql; // } // args[0]=completeSql; // } // // } private String getSchema() { return LocalContext.getContext().getAttachment(ContextAttributes.SCHEMA); } }