使用SpringAOP實現(xiàn)公共字段填充功能
概要
在新增員工或者新增菜品分類時需要設(shè)置創(chuàng)建時間、創(chuàng)建人、修改時間、修改人等字段,在編輯員工或者編輯菜品分類時需要設(shè)置修改時間、修改人等字段。這些字段屬于公共字段,也就是也就是在我們的系統(tǒng)中很多表中都會有這些字段,如下:
| 序號 | 字段名 | 含義 | 數(shù)據(jù)類型 |
|---|---|---|---|
| 1 | create_time | 創(chuàng)建時間 | datetime |
| 2 | create_user | 創(chuàng)建人id | bigint |
| 3 | update_time | 修改時間 | datetime |
| 4 | update_user | 修改人id | bigint |
而針對于這些字段,我們的賦值方式為:
1). 在新增數(shù)據(jù)時, 將createTime、updateTime 設(shè)置為當前時間, createUser、updateUser設(shè)置為當前登錄用戶ID。
2). 在更新數(shù)據(jù)時, 將updateTime 設(shè)置為當前時間, updateUser設(shè)置為當前登錄用戶ID。
我們使用AOP切面編程,實現(xiàn)功能增強,來完成公共字段自動填充功能。
技術(shù)細節(jié)
在實現(xiàn)公共字段自動填充,也就是在插入或者更新的時候為指定字段賦予指定的值,使用它的好處就是可以統(tǒng)一對這些字段進行處理,避免了重復(fù)代碼。在上述的問題分析中,我們提到有四個公共字段,需要在新增/更新中進行賦值操作, 具體情況如下:
| 序號 | 字段名 | 含義 | 數(shù)據(jù)類型 | 操作類型 |
|---|---|---|---|---|
| 1 | create_time | 創(chuàng)建時間 | datetime | insert |
| 2 | create_user | 創(chuàng)建人id | bigint | insert |
| 3 | update_time | 修改時間 | datetime | insert、update |
| 4 | update_user | 修改人id | bigint | insert、update |
實現(xiàn)步驟:
1). 自定義注解 AutoFill,用于標識需要進行公共字段自動填充的方法
2). 自定義切面類 AutoFillAspect,統(tǒng)一攔截加入了 AutoFill 注解的方法,通過反射為公共字段賦值
3). 在 Mapper 的方法上加入 AutoFill 注解
若要實現(xiàn)上述步驟,需掌握以下知識
技術(shù)點:枚舉、注解、AOP、反射
- 枚舉類:
package com.sky.enumeration;
/**
* 數(shù)據(jù)庫操作類型
*/
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;
/**
* 自定義注解,用于標識某個方法需要進行功能字段自動填充處理
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
//數(shù)據(jù)庫操作類型:UPDATE INSERT
OperationType value();
}- 自定義切面類:
- 公共字段填充,我們選擇在連接點(方法)執(zhí)行前就進行,所以用@Before
- 將連接點上的@AutoFill注解攔截下
- 利用注解的.value()獲取到注解中的內(nèi)容(數(shù)據(jù)庫操作類型)
- 獲取到該連接點的參數(shù)
- 準備好填充的內(nèi)容
- 利用反射根據(jù)數(shù)據(jù)庫操作類型進行填充
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 {
//切入點
@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFIll)")
public void autoFillPointCut() {
}
//前置通知,在通知中進行公共字段的賦值
@Before("autoFillPointCut()")
public void autoFill(JoinPoint joinPoint) {
log.info("進行公共字段賦值");
//獲取當前被攔截方法上的注解
AutoFIll autoFIll = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(AutoFIll.class);
//MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法簽名對象
//AutoFIll autoFill = signature.getMethod().getAnnotation(AutoFIll.class);
//獲取數(shù)據(jù)庫操作類型(INSERT,UPDATE)
OperationType operationType = autoFIll.value();
//獲取當前被攔截方法上的參數(shù)(實體對象)
Object[] args = joinPoint.getArgs();
if (args == null || args.length == 0) {
return;
}
Object entity = args[0];
//準備要填充的內(nèi)容
LocalDateTime now = LocalDateTime.now();
Long id = BaseContext.getCurrentId();
//根據(jù)數(shù)據(jù)庫操作類型進行賦值
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實現(xiàn)公共字段填充功能的文章就介紹到這了,更多相關(guān)SpringAOP公共字段填充內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis綁定錯誤提示BindingException:Invalid bound statement (not f
這篇文章主要介紹了MyBatis綁定錯誤提示BindingException:Invalid bound statement (not found)的解決辦法,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-01-01
2020新版idea創(chuàng)建項目沒有javaEE 沒有Web選項的完美解決方法
這篇文章主要介紹了2020新版idea創(chuàng)建項目沒有javaEE 沒有Web選項的完美解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
使用JPA+querydsl如何實現(xiàn)多條件動態(tài)查詢
這篇文章主要介紹了使用JPA+querydsl如何實現(xiàn)多條件動態(tài)查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java客戶端通過HTTPS連接到Easysearch實現(xiàn)過程
這篇文章主要為大家介紹了Java客戶端通過HTTPS連接到Easysearch實現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
SpringBoot異步調(diào)用方法實現(xiàn)場景代碼實例
這篇文章主要介紹了SpringBoot異步調(diào)用方法實現(xiàn)場景代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04

