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

所属分类: 网络编程 / JSP编程 阅读数: 1308
收藏 0 赞 0 分享

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();
 }
  } 

} 
 

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

更多精彩内容其他人还在看

weblogic 8.1下重新编译java类但不用重启服务器的方法

weblogic 8.1下重新编译java类但不用重启服务器的方法
收藏 0 赞 0 分享

JSP下动态INCLUDE与静态INCLUDE的区别分析

这篇文章给大家介绍了JSP下动态INCLUDE与静态INCLUDE的区别分析,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

jsp中文乱码 jsp mysql 乱码的解决方法

当使用JSP页面将中文数据添加到MySql数据库中的时候发现变为乱码,或者从mysql中读取中文的时候出现乱码,这些问题根源都是由于字符编码不一致造成的。本文介绍jsp mysql 乱码的解决方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Jsp页面实现文件上传下载类代码第1/2页

Jsp页面实现文件上传下载类代码
收藏 0 赞 0 分享

下载完成后页面不自动关闭的方法

其实就一句话js代码,window.close()
收藏 0 赞 0 分享

JBuilder2005实现重构

JBuilder2005实现重构
收藏 0 赞 0 分享

CORBA对象生命周期

CORBA对象生命周期
收藏 0 赞 0 分享

基于Java的代理设计模式

基于Java的代理设计模式
收藏 0 赞 0 分享

Java中四种XML解析技术

Java中四种XML解析技术
收藏 0 赞 0 分享

跨平台Java程序

跨平台Java程序
收藏 0 赞 0 分享
查看更多