切面中使用反射技术

package cn.com.demo.abby.common.aspect;

import cn.com.swancloud.aliyun.Authentication;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @Date: 2019/1/16 16:13
 * 生成认证对象切面
 */
@Aspect
@Component
public class AuthenticationAspect {

    private final String regionId = "regionId";
    private final String AccessKey_ID = "AccessKey_ID";
    private final String AccessKey_Secret = "AccessKey_Secret";

    /**
     * 对指定包,进行横切,创建一个横切的对象方法
     */
    @Pointcut("execution(* cn.com.demo.abby.service..*.*(..))")
    public void executionPoint() {}

    @Before("executionPoint()")
    public void before(JoinPoint joinPoint) {
        try {
            Object[] o = joinPoint.getArgs();
            for (int i = 0; i < o.length; i++) {

                //1.加载类
                String name = o[i].getClass().getName();
                Class clz = Class.forName(name);

                //2.获取当前类包含其父类的全部属性
                List<Field> fieldList = new ArrayList<>();
                while(clz != null){
                    fieldList.addAll(Arrays.asList(clz.getDeclaredFields()));
                    clz = clz.getSuperclass();
                }

                //3.匹配到iAcsClient属性,赋值
                for(Field field : fieldList){
                    if(("iAcsClient").equals(field.getName())){
                        //4.如果此属性为非public属性的情况时,需要设置属性可达,否则会抛出IllegalAccessException异常
                        field.setAccessible(true);
                        //5.生成认证对象
                        //5.1获取指定属性值
                        Map<String, String> fieldsValue = getFieldsValue(fieldList, o[i]);
                        //5.2创建 Profile。生成 IClientProfile 的对象 profile,该对象存放 AccessKeyID 和 AccessKeySecret 和默认的地域信息
                        DefaultProfile profile = DefaultProfile.getProfile(fieldsValue.get(regionId), fieldsValue.get(AccessKey_ID), fieldsValue.get(AccessKey_Secret));
                        //5.3创建 Client。从 IClientProfile 类中再生成 IAcsClient 的对象 client,后续获得 response 都需要从 IClientProfile 中获得。
                        IAcsClient client = new DefaultAcsClient(profile);
                        //6.赋值
                        field.set(o[i],client);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Map<String,String> getFieldsValue(List<Field> fieldList,Object object) throws Exception {
        Map<String,String> result = new HashMap<>();
        for(Field field : fieldList){
            if(("regionId").equals(field.getName())){
                //如果此属性为非public属性的情况时,需要设置属性可达,否则会抛出IllegalAccessException异常
                field.setAccessible(true);
                //获取属性值存入Map
                result.put(regionId,field.get(object).toString());
            }
            if(("authentication").equals(field.getName())){
                field.setAccessible(true);
                //获取属性值存入Map
                Authentication authentication = (Authentication)field.get(object);
                result.put(AccessKey_ID,authentication.getAccessKey_ID());
                result.put(AccessKey_Secret,authentication.getAccessKey_Secret());
            }
        }
        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/Peacock__/article/details/86512637