这是我之前做的日志代码用的是before和afterthrowing代码如下
package com.crm.admin.controller.advice;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.crm.admin.shiro.util.ShiroUserUtils;
import com.crm.bussiness.biz.log.ToUserLogsBiz;
import com.crm.bussiness.dao.entity.SysUser;
import com.crm.bussiness.dao.entity.log.ToUserLogs;
import com.crm.util.JsonUtils;
@Component
@Aspect
public class LogInterceptor {
@Autowired
ToUserLogsBiz toUserLogsBiz;
ToUserLogs tul;
@Pointcut("@annotation(com.crm.admin.controller.advice.Operate)")
public void invokePoint(){
}
@Before("invokePoint()")
public void beforeMethod(JoinPoint jp){
tul=new ToUserLogs();
SysUser user = ShiroUserUtils.getCurrentShiroUser().getSysUser();
String host = ShiroUserUtils.getSubject().getSession().getHost();
Operate operate = getOperate(jp);
tul.setOperatorInfo(operate.operatingContent());
tul.setOperatorTime(new Date());
tul.setRoleName(user.getRealName());
tul.setUserId(user.getId());
tul.setIp(host);
tul.setState(1);
Object[] objs = jp.getArgs();
List<String> params = new ArrayList<>();
for (Object object : objs) {
if (!(object instanceof HttpServletRequest) && !(object instanceof HttpServletResponse)) {
params.add(JsonUtils.beanToJson(object));
}
}
toUserLogsBiz.insert(tul);
}
@AfterThrowing(value="invokePoint()", throwing = "e")
public void afterThrow(JoinPoint jp,Exception e){
tul=new ToUserLogs();
tul.setState(0);
tul.setExceptionInfo(e.getMessage());
toUserLogsBiz.updateByPrimaryKeySelective(tul);
}
public Operate getOperate(JoinPoint pjp){
String methodName=pjp.getSignature().getName();
Class<?> classTarget=pjp.getTarget().getClass();
Class<?>[] par=((MethodSignature) pjp.getSignature()).getParameterTypes();
Method objMethod = null;
try {
objMethod = classTarget.getMethod(methodName, par);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Operate annotation = objMethod.getAnnotation(Operate.class);
return annotation;
}
}
然后执行到toUserLogsBiz.updateByPrimaryKeySelective(tul);时更新失败是不是此时已经没有connection了 然后没有办法我还了@around代码如下
package com.crm.admin.controller.advice;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.crm.admin.shiro.util.ShiroUserUtils;
import com.crm.bussiness.biz.log.ToUserLogsBiz;
import com.crm.bussiness.dao.entity.SysUser;
import com.crm.bussiness.dao.entity.log.ToUserLogs;
import com.crm.util.JsonUtils;
@Component
@Aspect
public class LogInterceptor {
@Autowired
ToUserLogsBiz toUserLogsBiz;
ToUserLogs tul;
@Around("@annotation(com.crm.admin.controller.advice.Operate)")
public void aroundPoint(ProceedingJoinPoint pjp){
System.out.println("进入环绕");
try {
tul=new ToUserLogs();
SysUser user = ShiroUserUtils.getCurrentShiroUser().getSysUser();
String host = ShiroUserUtils.getSubject().getSession().getHost();
Operate operate = getOperate(pjp);
tul.setOperatorInfo(operate.operatingContent());
tul.setOperatorTime(new Date());
tul.setRoleName(user.getRealName());
tul.setUserId(user.getId());
tul.setIp(host);
tul.setState(1);
Object[] objs = pjp.getArgs();
List<String> params = new ArrayList<>();
for (Object object : objs) {
if (!(object instanceof HttpServletRequest) && !(object instanceof HttpServletResponse)) {
params.add(JsonUtils.beanToJson(object));
}
}
toUserLogsBiz.insert(tul);
pjp.proceed();
} catch (Throwable e) {
tul.setState(0);
tul.setExceptionInfo(e.getMessage());
toUserLogsBiz.updateByPrimaryKeySelective(tul);
e.printStackTrace();
System.out.println(e.getMessage()+"---------------");
}
}
public Operate getOperate(ProceedingJoinPoint pjp){
String methodName=pjp.getSignature().getName();
Class<?> classTarget=pjp.getTarget().getClass();
Class<?>[] par=((MethodSignature) pjp.getSignature()).getParameterTypes();
Method objMethod = null;
try {
objMethod = classTarget.getMethod(methodName, par);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Operate annotation = objMethod.getAnnotation(Operate.class);
return annotation;
}
}
此时可以将异常信息写入 百思不得其解难道@afterthrowing和@around有本质却别吗
还有我第一段代码在controller手动抛出异常时可以正常写入数据库 但是从service抛出的异常就不能正常写入 好奇怪 希望有大神给小弟解答