自定義切面例項

2021-08-25 06:08:45 字數 1005 閱讀 6767

需求:日誌插入,比如乙個許可權平台管理,系統希望記錄每一條管理員操作許可權或應用的記錄,如修改許可權,新增許可權,刪除許可權,新增應用,修改應用,刪除應用等,將這些審計記錄存到資料庫,方便追蹤。

用面向切面程式設計,自定義切面例項

1.自定義審計註解,用來指定操作型別和操作目標

/**

* 審計

*/@target()

@retention(retentionpolicy.runtime)

@documented

@inherited

public @inte***ce audit

2.建立切面類auditaop

@aspect

@component

public class auditaop

/*** 審計

*/@before("controllermethodpointcut()")

public void before(joinpoint joinpoint)

}string ip = networkutil.getipaddress(request);

auditservice.addaudit(type, value, ip);

}}

3.建立審計服務類

/**

* 審計服務

*/@service

public class auditservice

audit.setopertype(type);

audit.setuserid(getuserid());

jdbcdao.insert(audit);

}private string getuserid()

string id = dbconfig.getuserid();

if (stringutils.isempty(id))

return id;

}}

springboot aop 自定義切面程式設計

本文結合springboot,實現切面程式設計。before 前置通知 在某連線點 joinpoint 之前執行的通知,但這個通知不能阻止連線點前的執行。after 後通知 當某連線點退出的時候執行的通知 不論是正常返回還是異常退出 afterreturning 返回後通知 在某連線點正常完成後執行...

自定義註解例項

1.定義乙個全域性註解 要求登入 要求登入註解 target retention retentionpolicy.runtime documented public inte ce needlogin target 表示的是註解可以使用的範圍,這裡用於描述方法和 描述類 介面 包括註解型別 或enu...

spring自定義切面執行順序的方法

使用spring框架的時候,用aop需要控制自己寫的切面的順序,例如如下切面 aspect order 1 會先掃瞄註解的值,如果註解的值找不到,再找getorder 的值 public class aspectjorderlow implements ordered before testpoin...