java Aop實(shí)現(xiàn)自動(dòng)填充字段值示例
需求
自動(dòng)填充更新時(shí)間創(chuàng)建時(shí)間,創(chuàng)建用戶和更新用戶。
自定義注解
OperationType類是一個(gè)枚舉
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface AutoFill{ OperationType value(); }
OperationType枚舉類
/** * 數(shù)據(jù)庫操作類型 */ public enum OperationType { /** * 更新操作 */ UPDATE, /** * 插入操作 */ INSERT }
使用aop并且聲明一個(gè)事務(wù)
package com.sky.aspect; import com.sky.annotation.AutoFill; import com.sky.constant.AutoFillConstant; import com.sky.context.BaseContext; import com.sky.enumeration.OperationType; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.time.LocalDateTime; import java.util.Objects; @Aspect @Component @Slf4j public class AutoFillAspect { @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)") public void pt() { } @Before("pt()") public void autoFill(JoinPoint joinPoint) { //獲取簽名對象 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //獲取方法上注解 AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class); OperationType value = annotation.value(); //解析注解是增加還是更新 Object[] args = joinPoint.getArgs(); if (Objects.isNull(args) || args.length == 0) { return; } Object target = args[0]; LocalDateTime now = LocalDateTime.now(); Long currentId = BaseContext.getCurrentId(); Class<?> clazz = target.getClass(); if (value == OperationType.INSERT) { try { Method setCreateTimeMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class); Method setCreateUserMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class); setCreateTimeMethod.invoke(target,now); setCreateUserMethod.invoke(target,currentId); autoFillUpdateMethod(now,currentId,clazz,target); } catch (Exception e) { e.printStackTrace(); } } else if (value == OperationType.UPDATE) { try { autoFillUpdateMethod(now,currentId,clazz,target); } catch (Exception e) { e.printStackTrace(); } } } private void autoFillUpdateMethod(LocalDateTime now, Long id, Class<?> clazz, Object target) throws Exception { Method setUpdateTimeMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class); Method setUpdateUserMethod = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class); setUpdateTimeMethod.invoke(target,now); setUpdateUserMethod.invoke(target,id); }
以上就是Aop實(shí)現(xiàn)自動(dòng)填充字段值的詳細(xì)內(nèi)容,更多關(guān)于Aop實(shí)現(xiàn)自動(dòng)填充字段值的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
IDEA生成servlet程序的實(shí)現(xiàn)步驟
這篇文章主要介紹了IDEA生成servlet程序的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03關(guān)于spring5的那些事:@Indexed 解密
這篇文章主要介紹了關(guān)于spring5的那些事:@Indexed 解密,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11- 本章具體介紹了HashMap、TreeMap兩種集合的基本使用方法和區(qū)別,圖解穿插代碼實(shí)現(xiàn)。?JAVA成仙路從基礎(chǔ)開始講,后續(xù)會(huì)講到JAVA高級,中間會(huì)穿插面試題和項(xiàng)目實(shí)戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03
Spring框架應(yīng)用的權(quán)限控制系統(tǒng)詳解
在本篇文章里小編給大家整理的是關(guān)于基于Spring框架應(yīng)用的權(quán)限控制系統(tǒng)的研究和實(shí)現(xiàn),需要的朋友們可以學(xué)習(xí)下。2019-08-08Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)隊(duì)列簡單定義與用法示例
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)隊(duì)列簡單定義與用法,簡要描述了循環(huán)隊(duì)列的概念、原理,并結(jié)合實(shí)例形式分析了java循環(huán)隊(duì)列的定義與使用方法,需要的朋友可以參考下2017-10-10Java線程安全和鎖Synchronized知識(shí)點(diǎn)詳解
在本篇文章里小編給大家分享的是關(guān)于Java線程安全和鎖Synchronized相關(guān)知識(shí)點(diǎn),有需要的朋友們可以參考下。2019-08-08Java中BeanUtils.copyProperties的11個(gè)坑總結(jié)
我們?nèi)粘i_發(fā)中,經(jīng)常涉及到DO、DTO、VO對象屬性拷貝賦值,很容易想到org.springframework.beans.BeanUtils的copyProperties,它會(huì)自動(dòng)通過反射機(jī)制獲取源對象和目標(biāo)對象的屬性,pyProperties,會(huì)有好幾個(gè)坑呢,本文將給大家總結(jié)一下遇到的坑,需要的朋友可以參考下2023-05-05Java springboot 配置文件與多環(huán)境配置與運(yùn)行優(yōu)先級
這篇文章主要介紹了Java springboot如何配置文件,進(jìn)行多環(huán)境配置,以及運(yùn)行優(yōu)先級,感興趣的小伙伴可以借鑒一下2023-04-04Spring @CrossOrigin 注解原理實(shí)現(xiàn)
這篇文章主要介紹了Spring @CrossOrigin 注解原理實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07mybatis plus 自動(dòng)轉(zhuǎn)駝峰配置小結(jié)
SpringBoot提供兩種配置Mybatis的方式,第一種是通過yml或application.properties文件開啟配置,第二種是使用自定義配置類,通過給容器添加一個(gè)ConfigurationCustomizer來實(shí)現(xiàn)更靈活的配置,這兩種方法可以根據(jù)項(xiàng)目需求和個(gè)人喜好選擇使用2024-10-10最新評論