利用spring AOP記錄用戶操作日志的方法示例
前言
最近項(xiàng)目已經(jīng)開(kāi)發(fā)完成,但發(fā)現(xiàn)需要加用戶操作日志,如果返回去加也不太現(xiàn)實(shí),所以使用springAOP來(lái)完成比較合適。下面來(lái)一起看看詳細(xì)的介紹:
注解工具類:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogAnnotation {
String operateModelNm() default "";
String operateFuncNm() default "";
String operateDescribe() default "";
}
切面類:
@Aspect
public class MyInterceptor {
@Pointcut("execution(** com.luchao.spring.test3.service.impl.*.*(..))")
private void anyMethod(){}//定義一個(gè)切入點(diǎn)
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println(name);
System.out.println("前置通知");
}
@AfterReturning("anyMethod()")
public void doAfter(){
System.out.println("后置通知");
}
@After("anyMethod()")
public void after(JoinPoint point){
System.out.println("最終通知");
}
@AfterThrowing("anyMethod()")
public void doAfterThrow(){
System.out.println("例外通知");
}
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
Method targetMethod = methodSignature.getMethod();
// System.out.println("classname:" + targetMethod.getDeclaringClass().getName());
// System.out.println("superclass:" + targetMethod.getDeclaringClass().getSuperclass().getName());
// System.out.println("isinterface:" + targetMethod.getDeclaringClass().isInterface());
// System.out.println("target:" + pjp.getTarget().getClass().getName());
// System.out.println("proxy:" + pjp.getThis().getClass().getName());
// System.out.println("method:" + targetMethod.getName());
Class[] parameterTypes = new Class[pjp.getArgs().length];
Object[] args = pjp.getArgs();
for(int i=0; i<args.length; i++) {
if(args[i] != null) {
parameterTypes[i] = args[i].getClass();
}else {
parameterTypes[i] = null;
}
}
//獲取代理方法對(duì)象
String methodName = pjp.getSignature().getName();
Method method = pjp.getSignature().getDeclaringType().getMethod(methodName, parameterTypes);
if(method.isAnnotationPresent(LogAnnotation.class)){
System.out.println("存在1");
}
//獲取實(shí)際方法對(duì)象,可以獲取方法注解等
Method realMethod = pjp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
if(realMethod.isAnnotationPresent(LogAnnotation.class)){
realMethod.getAnnotation(LogAnnotation.class).operateDescribe();
System.out.println("存在2");
}
System.out.println("進(jìn)入環(huán)繞通知");
Object object = pjp.proceed();//執(zhí)行該方法
System.out.println("退出方法");
return object;
}
}
配置類:
@Configurable
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.luchao.spring.test3")
public class test3Config {
@Bean
public MyInterceptor myInterceptor(){
return new MyInterceptor();
}
@Bean
public EncoreableIntroducer encoreableIntroducer(){
return new EncoreableIntroducer();
}
}
服務(wù)類:
@Component
public class PersonServiceBean implements PersonServer {
/**
* 保存方法
* @param name
*/
@LogAnnotation(operateModelNm = "測(cè)試方法", operateFuncNm = "保存方法")
public void save(String name) {
System.out.println("我是save方法");
}
/**
* 更新方法
* @param name
* @param id
*/
public void update(String name, Integer id) {
System.out.println("我是update()方法");
}
/**
* 獲取方法
* @param id
* @return
*/
public String getPersonName(Integer id) {
System.out.println("我是getPersonName()方法");
return "xxx";
}
}
測(cè)試方法:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = test3Config.class)
public class SpringAOPTest {
@Autowired
private PersonServer personServer;
@Test
public void inteceptorTest(){
Encoreable encoreable = (Encoreable)personServer;
encoreable.performEncore();
personServer.save("test");
}
}
在springAOP切面中使用的是代理,所以直接獲取的是代理對(duì)象,不能獲取真實(shí)對(duì)象的一些信息,如注解等。
//獲取代理方法對(duì)象 String methodName = pjp.getSignature().getName(); Method method = pjp.getSignature().getDeclaringType().getMethod(methodName, parameterTypes);
如果要獲取真實(shí)對(duì)象,獲取注解的信息,可以方便我們進(jìn)行判斷記錄。
//獲取實(shí)際方法對(duì)象,可以獲取方法注解等 Method realMethod = pjp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
這樣就完成了一個(gè)簡(jiǎn)單的操作日志記錄demo。另外,如果不是講某個(gè)方法設(shè)置切點(diǎn),可以ant風(fēng)格的切點(diǎn)切入方式,設(shè)置多個(gè)或所有方法。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Spring Boot2中如何優(yōu)雅地個(gè)性化定制Jackson實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Spring Boot2中如何優(yōu)雅地個(gè)性化定制Jackson實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Java應(yīng)用打包后運(yùn)行需要注意編碼問(wèn)題
這篇文章主要介紹了 Java應(yīng)用打包后運(yùn)行需要注意編碼問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-12-12
使Java的JButton文字隱藏功能的實(shí)現(xiàn)(不隱藏按鈕的前提)
這篇文章主要介紹了使Java的JButton文字隱藏功能的實(shí)現(xiàn)(不隱藏按鈕的前提),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
SpringBoot事務(wù)異步調(diào)用引發(fā)的bug解決
本文主要介紹了SpringBoot事務(wù)異步調(diào)用引發(fā)的bug解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置方法
本文詳細(xì)介紹了SpringCloud集成MybatisPlus實(shí)現(xiàn)MySQL多數(shù)據(jù)源配置的方法,包括在application.properties中配置多數(shù)據(jù)源,配置MybatisPlus,創(chuàng)建Mapper接口和使用多數(shù)據(jù)源等步驟,此外,還解釋了每一個(gè)配置項(xiàng)目的含義,以便讀者更好地理解和應(yīng)用2024-10-10
JAVA基本類型包裝類 BigDecimal BigInteger 的使用
Java 中預(yù)定義了八種基本數(shù)據(jù)類型,包括:byte,int,long,double,float,boolean,char,short,接下來(lái)文章小編將向大家介紹其中幾個(gè)類型的內(nèi)容,需要的朋友可以參考下文章2021-09-09
java如何用反射將一個(gè)對(duì)象復(fù)制給另一個(gè)對(duì)象
這篇文章主要介紹了java如何用反射將一個(gè)對(duì)象復(fù)制給另一個(gè)對(duì)象問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

