spring aop aspectj做日志问题。

Zhang895341748 2016-09-21 08:45:24
错误:
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.fyq.service.impl.LogServiceImpl$$EnhancerBySpringCGLIB$$17886844.insertLog(<generated>)
at com.fyq.service.impl.LogAspectServiceImpl.insertServiceCallCalls(LogAspectServiceImpl.java:75)
at sun.reflect.GeneratedMethodAccessor103.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:603)
at org.springframework.aop.aspectj.AspectJAfterReturningAdvice.afterReturning(AspectJAfterReturningAdvice.java:61)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:53)

at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at com.alibaba.druid.support.spring.stat.DruidStatInterceptor.invoke(DruidStatInterceptor.java:72)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.fyq.service.impl.LogServiceImpl$$EnhancerBySpringCGLIB$$17886844.insertLog(<generated>)
at com.fyq.service.impl.LogAspectServiceImpl.insertServiceCallCalls(LogAspectServiceImpl.java:75)
at sun.reflect.GeneratedMethodAccessor103.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:603)
at org.springframework.aop.aspectj.AspectJAfterReturningAdvice.afterReturning(AspectJAfterReturningAdvice.java:61)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:53)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at com.alibaba.druid.support.spring.stat.DruidStatInterceptor.invoke(DruidStatInterceptor.java:72)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.fyq.service.impl.LogServiceImpl$$EnhancerBySpringCGLIB$$17886844.insertLog(<generated>)
at com.fyq.service.impl.LogAspectServiceImpl.insertServiceCallCalls(LogAspectServiceImpl.java:75)
at sun.reflect.GeneratedMethodAccessor103.invoke(Unknown Source)

配置:
<aop:aspectj-autoproxy />
<bean id="logAspect" class="xxx.LogAspectServiceImpl" />

我的代码

@Aspect
public class LogAspectServiceImpl implements LogAspectService{

@Autowired
private LogService logService;


/**
* 添加业务逻辑方法切入点
*/
@Pointcut("execution(* com.fyq.service.*.insert*(..))")
public void insertServiceCall() {

}

/**
* 修改业务逻辑方法切入点
*/
@Pointcut("execution(* com.fyq.service.*.update*(..))")
public void updateServiceCall() {

}

/**
* 删除业务逻辑方法切入点
*/
@Pointcut("execution(* com.fyq.service.*.delete*(..))")
public void deleteServiceCall() {

}

/**
* 管理员添加操作日志(后置通知)
*/
@AfterReturning(value="insertServiceCall()", argNames="rtv", returning="rtv")
public void insertServiceCallCalls(JoinPoint joinPoint, Object rtv) throws Throwable{
Integer adminUserId=(Integer)request.getSession().getAttribute("userId"); // 获取当前用户的id
if(null==adminUserId){
return;
}
if(null==joinPoint.getArgs()){ // 判断参数
return;
}
String methodName=joinPoint.getSignature().getName(); // 获取方法名
String opContent=adminOptionContent(joinPoint.getArgs(), methodName); // 获取操作内容
String ip=IpAddress.getIpAddress(request);

log lg=new log();
lg.setCreateby(adminUserId);
lg.setCreated(new Date());
lg.setContent(opContent);
lg.setIp(ip);
lg.setRemark("添加");
logService.insertLog(lg);
}

/**
* 管理员修改操作日志(后置通知)
*/
@AfterReturning(value="updateServiceCall()", argNames="rtv", returning="rtv")
public void updateServiceCallCalls(JoinPoint joinPoint, Object rtv) throws Throwable{
Integer adminUserId=(Integer)request.getSession().getAttribute("userId");
if(null==adminUserId){
return;
}
if(null==joinPoint.getArgs()){ // 没有参数
return;
}
String methodName = joinPoint.getSignature().getName(); // 获取方法名
String opContent = adminOptionContent(joinPoint.getArgs(), methodName); // 获取操作内容
String ip=IpAddress.getIpAddress(request);

log lg=new log();
lg.setCreateby(adminUserId);
lg.setCreated(new Date());
lg.setContent(opContent);
lg.setIp(ip);
lg.setRemark("修改");
logService.insertLog(lg);
}

/**
* 环绕通知,使用环绕通知的目的是 被删除前可以先查询出影片信息用于日志记录
*/
@Around(value="deleteServiceCall()", argNames="rtv")
public Object deleteServiceCallCalls(ProceedingJoinPoint pjp) throws Throwable {
Integer adminUserId=(Integer)request.getSession().getAttribute("userId");
Object result = null;
try {
String id=String.valueOf(pjp.getArgs()[0]); // 获取方法参数(被删除的id)
String methodName=pjp.getSignature().getName();
String ip=IpAddress.getIpAddress(request);
result=pjp.proceed(); // 执行删除操作
log lg=new log();
lg.setCreateby(adminUserId);
lg.setCreated(new Date());
lg.setContent(methodName+" 参数id: " + id);
lg.setIp(ip);
lg.setRemark("删除");
logService.insertLog(lg);
}
catch(Exception ex) {
ex.printStackTrace();
}
return result;
}

/**
* 使用Java反射来获取被拦截方法(insert、update)的参数值,
* 将参数值拼接为操作内容
*/
public String adminOptionContent(Object[] args, String mName) throws Exception{
if (null==args) {
return null;
}
StringBuffer rs = new StringBuffer();
rs.append(mName);
String className = null;
int index = 1;
for (Object info : args) { // 遍历参数对象
className = info.getClass().getName(); // 获取对象类型
className = className.substring(className.lastIndexOf(".") + 1);
rs.append(" [参数" + index + ",类型:" + className + ",值:");

Method[] methods = info.getClass().getDeclaredMethods(); // 获取对象的所有方法
for (Method method : methods) { // 遍历方法,判断get方法
String methodName = method.getName();
// 判断是不是get方法
if (methodName.indexOf("get") == -1) {// 不是get方法
continue; // 不处理
}
Object rsValue=null;
try {
rsValue=method.invoke(info); // 调用get方法,获取返回值
if (null==rsValue) { // 没有返回值
continue;
}
} catch (Exception e) {
continue;
}
rs.append("(" + methodName + " : " + rsValue + ")"); // 将值加入内容中
}
rs.append("]");
index++;
}
return rs.toString();
}


}
...全文
233 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧