12、spring-反射

本文介绍了如何在Java中使用反射API获取和设置实体类的属性值,以及调用其方法,以实现在运行时动态操作对象。以Spring框架和Lombok库为例,展示了如何在切面编程中拦截并修改`Student`类的`creatBy`属性和调用`study`方法。
摘要由CSDN通过智能技术生成

● 获取class的三种当时
○ 对象.getClass()
○ 类名.class
○ Class.forName(“”)
● getName
● getDeclaredFields 返回值 Field[]
○ filed.set(对象,value) 设置值
○ filed.setAccessible()//暴力破解私有属性,是私有属性能设置值
● getDeclaredMethods 返回值 Method[]
○ 对象.方法名()以前 调用对象的方法
○ method.invoke(对象,参数) 现在 调用对象的方法

在这里插入图片描述


● 实体类

package cn.zhy.spring_04.entiity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private Integer id;
    private String username;
    private String sex;
    private String creatBy;

    public void study(){
        System.out.println("我在学习");
    }
}

● 利用反射给属性设置值

@Component
@Aspect
public class LogInterceptor {
    @Pointcut("execution( * cn.zhy.spring_04.service..*.insert*(..))")
    public void myMethod() {
        // 假设这个方法就是被代理的方法
    }

    @Before("myMethod()")
    public void beforeMethod(JoinPoint joinPoint) throws IllegalAccessException {
        //获取拦截方法的参数
        Object[] args = joinPoint.getArgs();
        System.out.println(args);
        for (Object arg : args) {
            //获取该类
            Class<?> aClass = arg.getClass();
//            System.out.println(aClass);
            //获取该类中的属性
            Field[] declaredFields = aClass.getDeclaredFields();
//            System.out.println(declaredFields);
            for (Field field : declaredFields) {
//                System.out.println(field);
                //给属性设置值
                if(field.getName().equals("creatBy")){
                    field.setAccessible(true);
                    field.set(arg,"admin");
                }
            }

        }


        System.out.println("方法执行前");
    }

    @After("myMethod()")
    public void afterMethod() {
        System.out.println("方法执行后");
    }

    //在环绕 增强中,我们可以给定一个参数,代表我们获取处理的切入点
    @Around("myMethod()")
    public void arround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前");
        //执行方法
        Object proceed = joinPoint.proceed();
        System.out.println("环绕后");
    }
}

打印结果
在这里插入图片描述

利用反射调用方法

    @Test
    public void insertStudent() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//        StudentImpl studentImpl = (StudentImpl) context.getBean("studentImpl");
        IStudentService bean = context.getBean(IStudentService.class);
        Student student1 = new Student();


        Class<? extends Student> aClass = student1.getClass();
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            if (declaredMethod.getName().equals("study")){
                declaredMethod.invoke(student1);
            }

        }
//        Method study = aClass.getDeclaredMethod("study");
//        study.invoke(student1);
        student1.setId(1);
        student1.setUsername("张三");
        student1.setSex("男");
        bean.insertStudent(student1);
    }

结果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值