我需要用aop实现一个日志功能,
在service层里面需要进行日志记录的方法上加上自己的annotation然后在aop里面读取annotation并保存到数据库中。问题:我在用Aspect时,只能有一个JoinPoint对象被注入进来,但是这个method中的参数有可能是泛型,所以我想通过反射调用来获得Method对象就会不成功,因为参数getClass得到的是原型,而不是实际参数,所以就会报错另:看了一下实现AfterReturningAdvice接口能直接获得Method对象,可是该怎么进行Annotation的配置呢
package com.yunkj.core.aop.log;import java.lang.reflect.Method;
import java.lang.reflect.Type;import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import com.yunkj.base.entity.Loginfo;
import com.yunkj.base.entity.Userinfo;
import com.yunkj.base.service.LoginfoManager;
import com.yunkj.core.utils.IpUtils;
import com.yunkj.core.web.constants.YunkjConstants;@Component
@Aspect
public class LoginfoAop {
@Autowired
private LoginfoManager loginfoService; @AfterReturning("@annotation(com.yunkj.core.aop.log.GoaAnnotation)")
public void writeLogInfo(JoinPoint joinPoint) throws Exception,
IllegalAccessException {
// try{
Loginfo loginfo = new Loginfo();
Userinfo sysUser = (Userinfo) ServletActionContext.getRequest()
.getSession().getAttribute(YunkjConstants.SESSION_USERINFO);
String classType = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();

Class<?>[] Parameter = new Class[args.length];
for (int i = 0; i < args.length; i++) {
                    //这个得到方法的参数,是原型,而不是实际参数,
                    //比如page<Companyinfo>,那么就成了page.class!
Parameter[i] = args[i].getClass();
}

Class<?> className = Class.forName(classType);               //在这里就会报错,因为给的Parameter是原型,而不是泛型的实际参数,就会报找不到这个参数
Method method = className.getMethod(methodName, Parameter); if (method.isAnnotationPresent(GoaAnnotation.class)) {
GoaAnnotation goaAnnotation = method
.getAnnotation(GoaAnnotation.class);
String operateDescribe = goaAnnotation.operateDescribe(); // 判断用户,如果为空的话,就直接用IP进行读写
String UserName = null;
if (sysUser == null) {
UserName = IpUtils.getIpAddr(ServletActionContext.getRequest());
} else
UserName = sysUser.getName(); loginfo.setUsername(UserName);
// 信息
loginfo.setLogInfo("用户:" + UserName + "进行了" + operateDescribe
+ "操作"); loginfo.setUserip(IpUtils.getIpAddr(ServletActionContext
.getRequest())); loginfoService.save(loginfo);
}
}}