欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP 自定义注解及记录操作日志

程序员文章站 2023-11-24 20:13:28
jsp 自定义注解及记录操作日志 spring的配置文件 日志拦截器 pac...

jsp 自定义注解及记录操作日志

spring的配置文件

<aop:aspectj-autoproxy />

日志拦截器

package com.vem.interceptor;

import org.aspectj.lang.proceedingjoinpoint;
import org.aspectj.lang.annotation.around; 
import org.aspectj.lang.annotation.aspect; 
import org.aspectj.lang.annotation.pointcut;
import org.aspectj.lang.reflect.methodsignature;
import org.springframework.stereotype.component;

import com.vem.entity.bussannotation; 

@aspect 
@component 
public class loginterceptor { 
 
 
  @pointcut("execution(* com.vem.service..*.*(..))") 
  public void aapplogic() { 
   
  } 
   
  /** 
   * 环绕通知 用于拦截指定内容,记录用户的操作 
   */ 
  @around(value = "aapplogic() && @annotation(annotation) &&args(object,..) ", argnames = "annotation,object") 
  public void interceptorapplogic(proceedingjoinpoint joinpoint, 
      bussannotation annotation, object object) throws throwable { 
    system.out.println("模块名称modulename:" + annotation.modulename()); 
    system.out.println("操作名称option:" + annotation.option()); 
    string methodname = joinpoint.getsignature().getname();
 system.out.println("方法名methodname:" + methodname); 
    
    methodsignature methodsignature = (methodsignature) joinpoint.getsignature(); 
    string[] strings = methodsignature.getparameternames(); 
    
    joinpoint.proceed(); 
    
    object[] arguments = joinpoint.getargs();  //获得参数列表
    if(arguments.length<=0){ 
      system.out.println(methodname+"方法没有参数"); 
    }else{ 
     for(int i=0;i<arguments.length;i++){ 
     system.out.println(strings[i]+" : "+arguments[i]+" : ");
     } 
    } 
  } 
} 

自定义注解

@retention(retentionpolicy.runtime) 
@target({elementtype.method}) 
@documented 
public @interface bussannotation { 
  //模块名 
  string modulename() default ""; 
  //操作内容 
  string option() default ""; 
} 

接口实现

写在service

@bussannotation(modulename="人员管理",option="添加用户") 
public void testdemo1(pagedata pd) throws exception{
 
}

junit测试类

package com.vem.entity;

import javax.annotation.resource;

import org.junit.test; 
import org.junit.runner.runwith; 
import org.springframework.test.context.contextconfiguration; 
import org.springframework.test.context.junit4.springjunit4classrunner;

import com.vem.service.data.demoservice;
import com.vem.util.pagedata; 
 
@runwith(springjunit4classrunner.class) 
@contextconfiguration(
 {"classpath:spring/applicationcontext.xml"
 })
public class aoptest { 
 
 @resource(name = "demoservice")
 public demoservice demoservice;
  
  @test 
  public void testaopadduser1(){ 
   pagedata pd = new pagedata();
   pd.put("name", "zhangzexing");
   pd.put("age", "21");
   pd.put("passward", "123456");
   try {
  demoservice.testdemo2(pd);
 } catch (exception e) {
  // todo auto-generated catch block
  e.printstacktrace();
 }
  } 

} 
 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!