JSP自定义注解及记录操作日志
Spring的配置文件
<aop:aspectj-autoproxy/>
日志拦截器
packagecom.vem.interceptor;
importorg.aspectj.lang.ProceedingJoinPoint;
importorg.aspectj.lang.annotation.Around;
importorg.aspectj.lang.annotation.Aspect;
importorg.aspectj.lang.annotation.Pointcut;
importorg.aspectj.lang.reflect.MethodSignature;
importorg.springframework.stereotype.Component;
importcom.vem.entity.BussAnnotation;
@Aspect
@Component
publicclassLogInterceptor{
@Pointcut("execution(*com.vem.service..*.*(..))")
publicvoidaApplogic(){
}
/**
*环绕通知用于拦截指定内容,记录用户的操作
*/
@Around(value="aApplogic()&&@annotation(annotation)&&args(object,..)",argNames="annotation,object")
publicvoidinterceptorApplogic(ProceedingJoinPointjoinPoint,
BussAnnotationannotation,Objectobject)throwsThrowable{
System.out.println("模块名称moduleName:"+annotation.moduleName());
System.out.println("操作名称option:"+annotation.option());
StringmethodName=joinPoint.getSignature().getName();
System.out.println("方法名methodName:"+methodName);
MethodSignaturemethodSignature=(MethodSignature)joinPoint.getSignature();
String[]strings=methodSignature.getParameterNames();
joinPoint.proceed();
Object[]arguments=joinPoint.getArgs();//获得参数列表
if(arguments.length<=0){
System.out.println(methodName+"方法没有参数");
}else{
for(inti=0;i<arguments.length;i++){
System.out.println(strings[i]+":"+arguments[i]+":");
}
}
}
}
自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public@interfaceBussAnnotation{
//模块名
StringmoduleName()default"";
//操作内容
Stringoption()default"";
}
接口实现
写在service
@BussAnnotation(moduleName="人员管理",option="添加用户")
publicvoidtestDemo1(PageDatapd)throwsException{
}
junit测试类
packagecom.vem.entity;
importjavax.annotation.Resource;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
importcom.vem.service.data.DemoService;
importcom.vem.util.PageData;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
{"classpath:spring/ApplicationContext.xml"
})
publicclassAopTest{
@Resource(name="demoService")
publicDemoServicedemoService;
@Test
publicvoidtestAopAddUser1(){
PageDatapd=newPageData();
pd.put("name","zhangzexing");
pd.put("age","21");
pd.put("passward","123456");
try{
demoService.testDemo2(pd);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
|