JAVA:反射和AOP实现

Java 中的反射机制是指在运行状态中,对于任意一个类都能够知道这个类所有的属性和方法;
并且对于任意一个对象,都能够调用它的任意一个方法;这种动态获取信息以及动态调用对象方
法的功能成为 Java 语言的反射机制。

反射的应用场合
在 Java 程序中许多对象在运行是都会出现两种类型:编译时类型和运行时类型。 编译时的类型由
声明对象时实用的类型来决定,运行时的类型由实际赋值给对象的类型决定 。如:
Person p=new Student();
其中编译时类型为 Person,运行时类型为 Student。
程序在运行时还可能接收到外部传入的对象,该对象的编译时类型为 Object,但是程序有需要调用
该对象的运行时类型的方法。为了解决这些问题,程序需要在运行时发现对象和类的真实信息。
然而,如果编译时根本无法预知该对象和类属于哪些类,程序只能依靠运行时信息来发现该对象
和类的真实信息,此时就必须使用到反射了。

Java 反射 API
反射 API 用来生成 JVM 中的类、接口或则对象的信息。

  1. Class 类:反射的核心类,可以获取类的属性,方法等信息。
  2. Field 类:Java.lang.reflec 包中的类,表示类的成员变量,可以用来获取和设置类之中的属性
    值。
  3. Method 类: Java.lang.reflec 包中的类,表示类的方法,它可以用来获取类中的方法信息或
    者执行方法。
  4. Constructor 类: Java.lang.reflec 包中的类,表示类的构造方法。
    反射使用步骤(获取 Class 对象、调用对象方法)
    1.获取想要操作的类的 Class 对象,他是反射的核心,通过 Class 对象我们可以任意调用类的方法。
    2.调用 Class 类中的方法,既就是反射的使用阶段。
    3.使用反射 API 来操作这些信息。

获取 Class 对象的 3 种方法

调用某个对象的 getClass()方法
Person p=new Person();
Class clazz=p.getClass();
调用某个类的 class 属性来获取该类对应的 Class 对象
Class clazz=Person.class;
使用 Class 类中的 forName()静态方法(最安全/性能最好)
Class clazz=Class.forName("类的全路径"); (最常用)
//当我们获得了想要操作的类的 Class 对象后,可以通过 Class 类中的方法获取并查看该类中的方法和属性。
//获取 Person 类的 Class 对象
 Class clazz=Class.forName("reflection.Person");
 //获取 Person 类的所有方法信息
 Method[] method=clazz.getDeclaredMethods();
 for(Method m:method){
 System.out.println(m.toString());
 }
 //获取Person类的某个方法
 Method m = clazz.getDeclaredMethods(name:"study",String.class);//方法名+参数
 Person p = new Person();
 m.invoke(p,方法的参数);
 //获取 Person 类的所有成员属性信息
 Field[] field=clazz.getDeclaredFields();
 for(Field f:field){
 System.out.println(f.toString());
 }
 //获取 Person 类的所有构造方法信息
 Constructor[] constructor=clazz.getDeclaredConstructors();
 for(Constructor c:constructor){
 System.out.println(c.toString());
 }
 //获取Person类中所有public构造方法
 Constructor[] constructor=clazz.getConstructors();
 for(Constructor c:constructor){
 System.out.println(c.toString());
 }
 //获取某一个构造方法
 Constructor[] constructor=clazz.getDeclaredConstructors(String.class,int .class);
 for(Constructor c:constructor){
 System.out.println(c.toString());
 }

创建对象的两种方法

调用 Constructor 对象的 newInstance()
先使用 Class 对象获取指定的 Constructor 对象,再调用 Constructor 对象的 newInstance()
方法来创建 Class 对象对应类的实例,通过这种方法可以选定构造方法创建实例。

//获取构造方法并创建对象
 Constructor c=clazz.getDeclaredConstructor(String.class,String.class,int.class);
 //创建对象并设置属性
 Person p1=(Person) c.newInstance("李四","男",20);

AOP实现

切入点:有两种方法指定

  1. execution:返回值 包名.类名.方法名(参数)
  2. @annotation(声明的注解全类名)
@Target(ElementType.METHOD)//目标
@Retention(RetentionPolicy.RUNTIME)//作用时机
public @interface AutoFill {
    OperationType value();
}
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {}

连接点:切面管理的所有方法
作用时机:
@After 方法运行后,连接点为JointPoint
@Before 方法运行前,连接点为JointPoint
@Around 环绕,连接点为ProceedingJointPoint
连接点操作方法

String objName=joinPoint.getTarget().getClass().getName();//获取目标对象类名
String name=joinPoint.getSignature().getName();//获取目标方法名
Object[] args=joinPoint.getArgs();//获取方法传入参数
Object result=joinPoint.proceed();//目标方法执行只有在Around时才生效并返回结果
 @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始进行公共字段填充");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();
        Object[] args = joinPoint.getArgs();


        if (args == null || args.length == 0) return;
        LocalDateTime now = LocalDateTime.now();
        Long curId = BaseContext.getCurrentId();
        Object entity = args[0];//获取对象
        if (operationType == OperationType.INSERT) {
            try {
                Method setCreateUser=entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME,Long.class);
                Method setUpdateTime=entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                Method setUpdateUser=entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
                Method setCreateTime=entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER,LocalDateTime.class);


                setCreateTime.invoke(entity,now);
                setUpdateTime.invoke(entity,now);
                setCreateUser.invoke(entity,curId);
                setUpdateUser.invoke(entity,curId);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
        if (operationType == OperationType.UPDATE) {
            try {
                Method setUpdateUser=entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
                Method setUpdateTime=entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
                setUpdateTime.invoke(entity,now);
                setUpdateUser.invoke(entity,curId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }
@Around("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public Object test(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("方法执行前");
        Object result = proceedingJoinPoint.proceed();
        log.info("方法执行后");
        return result;
        
    }
  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值