栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringAop+自定义注解实现基础属性自动注入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringAop+自定义注解实现基础属性自动注入

  • 功能: 通过自定义注解和springAop切面完成数据维护时基础数据的自动赋值

  • 在数据操作的时候避免不了基础数据的赋值,需要手动的赋值进去。比如上图的更新时间(update_time),和创建时间(create_time)在添加数据的时候就需要给创建时间手动添加一个当前时间,在更新数据的时候就需要给更新时间手动添加一个当前时间,虽然实现自动注入的方式有很多种,比如mybaits-plus就可以实现,今天在这里使用SpringAop和自定义注解完成自动注入。

  • 先创建自定义注解DefaultParams

  • 创建SpringAop切面类

package com.itheima.sms.aspect;

import com.itheima.pinda.context.baseContextHandler;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;


@Component
@Aspect
@Slf4j
public class DefaultParamsAspect {
    @SneakyThrows
    @Before("@annotation(com.itheima.sms.annotation.DefaultParams)")
    public void beforeEvent(JoinPoint point) {
        // TODO 自动注入基础属性(创建者、创建时间、修改者、修改时间)

        //从ThreadLocal中获得当前登录用户的id
        Long userId = baseContextHandler.getUserId();
        if(userId == null){
            userId = 0L;
        }

        //获得Controller方法的参数
        Object[] args = point.getArgs();
        //变量参数
        for(int i=0;i classes = entity.getClass();

            //获得实体中id属性值

            //获得getId方法对象
            Method method = getMethod(classes, "getId");
            if(method != null){
                //通过反射调用方法(getId)
                Object id = method.invoke(entity);
                if(id == null){
                    //当前进行的是新增操作,需要设置创建人createUser和创建时间createTime
                    method = getMethod(classes, "setCreateUser",String.class);
                    if(method != null){
                        method.invoke(entity,userId.toString());
                    }
                    method = getMethod(classes, "setCreateTime",LocalDateTime.class);
                    if(method != null){
                        method.invoke(entity,LocalDateTime.now());
                    }
                }

                method = getMethod(classes, "setUpdateUser",String.class);
                if(method != null){
                    method.invoke(entity,userId.toString());
                }

                method = getMethod(classes, "setUpdateTime",LocalDateTime.class);
                if(method != null){
                    method.invoke(entity,LocalDateTime.now());
                }
            }
        }

        System.out.println(point);
    }

    
    private Method getMethod(Class classes, String name, Class... types) {
        try {
            return classes.getMethod(name, types);
        } catch (NoSuchMethodException e) {
            return null;
        }
    }
}

  • 这样在需要自动注入的方法的上面添加我们的自定义注解就会自动给更新时间和创建时间注入数据
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号