使用SpringAOP實(shí)現(xiàn)公共字段填充功能
概要
在新增員工或者新增菜品分類時(shí)需要設(shè)置創(chuàng)建時(shí)間、創(chuàng)建人、修改時(shí)間、修改人等字段,在編輯員工或者編輯菜品分類時(shí)需要設(shè)置修改時(shí)間、修改人等字段。這些字段屬于公共字段,也就是也就是在我們的系統(tǒng)中很多表中都會(huì)有這些字段,如下:
| 序號(hào) | 字段名 | 含義 | 數(shù)據(jù)類型 |
|---|---|---|---|
| 1 | create_time | 創(chuàng)建時(shí)間 | datetime |
| 2 | create_user | 創(chuàng)建人id | bigint |
| 3 | update_time | 修改時(shí)間 | datetime |
| 4 | update_user | 修改人id | bigint |
而針對(duì)于這些字段,我們的賦值方式為:
1). 在新增數(shù)據(jù)時(shí), 將createTime、updateTime 設(shè)置為當(dāng)前時(shí)間, createUser、updateUser設(shè)置為當(dāng)前登錄用戶ID。
2). 在更新數(shù)據(jù)時(shí), 將updateTime 設(shè)置為當(dāng)前時(shí)間, updateUser設(shè)置為當(dāng)前登錄用戶ID。
我們使用AOP切面編程,實(shí)現(xiàn)功能增強(qiáng),來(lái)完成公共字段自動(dòng)填充功能。
技術(shù)細(xì)節(jié)
在實(shí)現(xiàn)公共字段自動(dòng)填充,也就是在插入或者更新的時(shí)候?yàn)橹付ㄗ侄钨x予指定的值,使用它的好處就是可以統(tǒng)一對(duì)這些字段進(jìn)行處理,避免了重復(fù)代碼。在上述的問題分析中,我們提到有四個(gè)公共字段,需要在新增/更新中進(jìn)行賦值操作, 具體情況如下:
| 序號(hào) | 字段名 | 含義 | 數(shù)據(jù)類型 | 操作類型 |
|---|---|---|---|---|
| 1 | create_time | 創(chuàng)建時(shí)間 | datetime | insert |
| 2 | create_user | 創(chuàng)建人id | bigint | insert |
| 3 | update_time | 修改時(shí)間 | datetime | insert、update |
| 4 | update_user | 修改人id | bigint | insert、update |
實(shí)現(xiàn)步驟:
1). 自定義注解 AutoFill,用于標(biāo)識(shí)需要進(jìn)行公共字段自動(dòng)填充的方法
2). 自定義切面類 AutoFillAspect,統(tǒng)一攔截加入了 AutoFill 注解的方法,通過反射為公共字段賦值
3). 在 Mapper 的方法上加入 AutoFill 注解
若要實(shí)現(xiàn)上述步驟,需掌握以下知識(shí)
技術(shù)點(diǎn):枚舉、注解、AOP、反射
- 枚舉類:
package com.sky.enumeration;
/**
* 數(shù)據(jù)庫(kù)操作類型
*/
public enum OperationType {
/**
* 更新操作
*/
UPDATE,
/**
* 插入操作
*/
INSERT
}- 自定義注解:
package com.sky.annotation;
import com.sky.enumeration.OperationType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定義注解,用于標(biāo)識(shí)某個(gè)方法需要進(jìn)行功能字段自動(dòng)填充處理
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
//數(shù)據(jù)庫(kù)操作類型:UPDATE INSERT
OperationType value();
}- 自定義切面類:
- 公共字段填充,我們選擇在連接點(diǎn)(方法)執(zhí)行前就進(jìn)行,所以用@Before
- 將連接點(diǎn)上的@AutoFill注解攔截下
- 利用注解的.value()獲取到注解中的內(nèi)容(數(shù)據(jù)庫(kù)操作類型)
- 獲取到該連接點(diǎn)的參數(shù)
- 準(zhǔn)備好填充的內(nèi)容
- 利用反射根據(jù)數(shù)據(jù)庫(kù)操作類型進(jìn)行填充
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.annotation.Annotation;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
@Aspect
@Slf4j
@Component
public class AutoFillAspect {
//切入點(diǎn)
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFIll)")
public void autoFillPointCut() {
}
//前置通知,在通知中進(jìn)行公共字段的賦值
@Before("autoFillPointCut()")
public void autoFill(JoinPoint joinPoint) {
log.info("進(jìn)行公共字段賦值");
//獲取當(dāng)前被攔截方法上的注解
AutoFIll autoFIll = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(AutoFIll.class);
//MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法簽名對(duì)象
//AutoFIll autoFill = signature.getMethod().getAnnotation(AutoFIll.class);
//獲取數(shù)據(jù)庫(kù)操作類型(INSERT,UPDATE)
OperationType operationType = autoFIll.value();
//獲取當(dāng)前被攔截方法上的參數(shù)(實(shí)體對(duì)象)
Object[] args = joinPoint.getArgs();
if (args == null || args.length == 0) {
return;
}
Object entity = args[0];
//準(zhǔn)備要填充的內(nèi)容
LocalDateTime now = LocalDateTime.now();
Long id = BaseContext.getCurrentId();
//根據(jù)數(shù)據(jù)庫(kù)操作類型進(jìn)行賦值
if (operationType == OperationType.INSERT) {
try {
Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
setCreateTime.invoke(entity, now);
setCreateUser.invoke(entity, id);
setUpdateTime.invoke(entity, now);
setUpdateUser.invoke(entity, id);
} catch (Exception e) {
e.printStackTrace();
}
} else if (operationType == operationType.UPDATE) {
try {
Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
setUpdateTime.invoke(entity, now);
setUpdateUser.invoke(entity, id);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}@AutoFIll(value = OperationType.INSERT)
void insert(Dish dish);效果展示

到此這篇關(guān)于使用SpringAOP實(shí)現(xiàn)公共字段填充功能的文章就介紹到這了,更多相關(guān)SpringAOP公共字段填充內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springsecurity自定義登錄頁(yè)面的實(shí)現(xiàn)示例
本文主要介紹了springsecurity自定義登錄頁(yè)面的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-09-09
Java系統(tǒng)性能優(yōu)化的五個(gè)實(shí)戰(zhàn)技巧
在迭代任務(wù)繁多、需求緊張、需求復(fù)雜的情況下,有時(shí)候開發(fā)人員為了追趕需求,而不考慮設(shè)計(jì)模式、設(shè)計(jì)原則、系統(tǒng)性能等,給系統(tǒng)埋了很多坑,欠下了許多技術(shù)債,下面介紹我所了解的Java性能優(yōu)化的一些技術(shù)手段,需要的朋友可以參考下2025-07-07
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(35)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧,希望可以幫到你2021-07-07
Springboot之idea之pom文件圖標(biāo)不對(duì)問題
這篇文章主要介紹了Springboot之idea之pom文件圖標(biāo)不對(duì)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
SpringBoot接口參數(shù)校驗(yàn)(Bean Validation)實(shí)戰(zhàn)指南
在開發(fā)接口時(shí),參數(shù)校驗(yàn)是必不可少的環(huán)節(jié):前端傳參是否為空、格式是否正確、數(shù)值是否合法,都需要后端嚴(yán)格校驗(yàn),否則很容易出現(xiàn)臟數(shù)據(jù)、程序異常,SpringBoot 官方推薦使用?Bean Validation,今天就來(lái)介紹一下基礎(chǔ)注解、實(shí)戰(zhàn)使用,需要的朋友可以參考下2026-03-03
java ConcurrentHashMap分段加鎖提高并發(fā)效率
這篇文章主要為大家介紹了java ConcurrentHashMap分段加鎖提高并發(fā)效率,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
SpringMVC中DispatcherServlet的HandlerMapping詳解
這篇文章主要介紹了SpringMVC中DispatcherServlet的HandlerMapping詳解,上回說的Handler,我們說是處理特定請(qǐng)求的,也就是說,不是所有的請(qǐng)求都能處理,那么問題來(lái)了,我們?cè)踔滥膫€(gè)請(qǐng)求是由哪個(gè)Handler處理的呢,需要的朋友可以參考下2023-10-10

