unable to install breakpoint in异常解决求助

夜雨~听风 2015-03-13 09:45:04




错误发生过程:现在做了一个spring mvc架构的项目,我添想添加一个可以控制打印前置log、后置log、异常log的控制器。
spring-mvc.xml配置如下:
方法一:
<bean id="operationLog" class="com.sj.framework.log.OperationLogAspectJ"></bean>
<aop:config>
<!--调用日志类-->
<aop:aspect id="b" ref="operationLog">
<!--配置在log包下所有的类在调用之前都会被拦截-->
<aop:pointcut id="log" expression="execution(* com.sj.framework.controller.*.*(..))"/>
<!--在log包下面所有的类的所有方法被调用之前都调用operationLog中的before方法-->
<aop:before pointcut-ref="log" method="doBefore"/>
</aop:aspect>
</aop:config>
方法二:
<!-- 拦截器 -->
<!-- Spring 统一日志处理 LogInterceptor拦截器 配置 -->
<bean id="logInterceptor" class="com.sj.framework.interceptor.LogInterceptor"/>
<!-- Spring 统一异常处理 ExceptionAdvisor配置 -->
<bean id="exceptionHandler" class="com.sj.framework.interceptor.ExceptionAdvisor"></bean>

<!-- Bean自动代理处理器 配置-->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
<property name="beanNames">
<list>
<value>*Controller</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>exceptionHandler</value>
<value>logInterceptor</value>
</list>
</property>
</bean>

java代码如下:
public class LogInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) {
Object obj;
try {
Logger loger = Logger.getLogger(invocation.getClass());

loger.info("-------------------------------------------------------------------------------------------------");
// 方法前的操作
loger.info(invocation.getMethod() + ":Start");
// 执行需要Log的方法
obj = invocation.proceed();

// 方法后的操作
loger.info(invocation.getMethod() + ":End");
loger.info("-------------------------------------------------------------------------------------------------");
return obj;
} catch (Throwable e) {
e.printStackTrace();
return "";
}
}
}


public class ExceptionAdvisor implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target,
Exception ex) throws Throwable {
// 在后台中输出错误异常异常信息,通过log4j输出。
Logger log = Logger.getLogger(target.getClass());
log.info("**************************************************************");
log.info("Error happened in class: " + target.getClass().getName());
log.info("Error happened in method: " + method.getName());
for (int i = 0; i < args.length; i++) {
log.info("arg[" + i + "]: " + args[i]);
}
log.info("Exception class: " + ex.getClass().getName());
log.info("ex.getMessage():" + ex.getMessage());
ex.printStackTrace();
log.info("**************************************************************");

// 在这里判断异常,根据不同的异常返回错误。
if (ex.getClass().equals(DataAccessException.class)) {
ex.printStackTrace();
throw new BusinessException("数据库操作失败!");
} else if (ex.getClass().toString()
.equals(NullPointerException.class.toString())) {
ex.printStackTrace();
throw new BusinessException("调用了未经初始化的对象或者是不存在的对象!");
} else if (ex.getClass().equals(IOException.class)) {
ex.printStackTrace();
throw new BusinessException("IO异常!");
} else if (ex.getClass().equals(ClassNotFoundException.class)) {
ex.printStackTrace();
throw new BusinessException("指定的类不存在!");
} else if (ex.getClass().equals(ArithmeticException.class)) {
ex.printStackTrace();
throw new BusinessException("数学运算异常!");
} else if (ex.getClass().equals(ArrayIndexOutOfBoundsException.class)) {
ex.printStackTrace();
throw new BusinessException("数组下标越界!");
} else if (ex.getClass().equals(IllegalArgumentException.class)) {
ex.printStackTrace();
throw new BusinessException("方法的参数错误!");
} else if (ex.getClass().equals(ClassCastException.class)) {
ex.printStackTrace();
throw new BusinessException("类型强制转换错误!");
} else if (ex.getClass().equals(SecurityException.class)) {
ex.printStackTrace();
throw new BusinessException("违背安全原则异常!");
} else if (ex.getClass().equals(SQLException.class)) {
ex.printStackTrace();
throw new BusinessException("操作数据库异常!");
} else if (ex.getClass().equals(NoSuchMethodError.class)) {
ex.printStackTrace();
throw new BusinessException("方法末找到异常!");
} else if (ex.getClass().equals(InternalError.class)) {
ex.printStackTrace();
throw new BusinessException("Java虚拟机发生了内部错误");
} else {
ex.printStackTrace();
throw new BusinessException("程序内部错误,操作失败!" + ex.getMessage());
}
}
}



public class OperationLogAspectJ {

// 注入Service用于把日志保存数据库
// @Resource
// private LogService logService;
// 本地异常日志记录对象
private static final Logger logger = LoggerFactory
.getLogger(OperationLogAspectJ.class);

// Controller层切点
@Pointcut("@annotation(com.sj.framework.log.SystemControllerLog)")
public void controllerAspect() {
}

// Service层切点
@Pointcut("@annotation(com.sj.framework.log.SystemServiceLog)")
public void serviceAspect() {
}

/**
* 前置通知 用于拦截Controller层记录用户的操作
*
* @param joinPoint
* 切点
*/
@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) {
logger.debug("==前置通知 Start==");
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
// HttpSession session = request.getSession();
// 读取session中的用户
// User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
// 请求的IP
String ip = request.getRemoteAddr();
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
try {
OperationLogInfo logInfo = new OperationLogInfo();
logInfo.setLogType("0");
logInfo.setUserId("");
logInfo.setMethod(className + "." + methodName);
logInfo.setCreateDatetime(new Date(0));
logInfo.setContent("");
logInfo.setIp(ip);
// 保存数据库
// logService.add(log);
logger.debug("==前置通知 End==");
} catch (Exception e) {
// 记录本地异常日志
logger.error("==前置通知异常==");
logger.error("异常信息:{}", e.getMessage());
}
}

/**
* 异常通知 用于拦截service层记录异常日志
*
* @param joinPoint
* @param e
*/
@AfterThrowing(pointcut = "serviceAspect()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
logger.debug("==异常通知 Start==");
// HttpSession session = request.getSession();
// 读取session中的用户
// User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
// 获取请求ip
String ip = request.getRemoteAddr();
// 获取用户请求方法的参数并序列化为JSON格式字符串
String params = "";
if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {
for (int i = 0; i < joinPoint.getArgs().length; i++) {
params += JsonUtil.bean2Json(joinPoint.getArgs()[i]) + ";";
}
}
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
try {
OperationLogInfo logInfo = new OperationLogInfo();
logInfo.setLogType("0");
logInfo.setUserId("");
logInfo.setMethod(className + "." + methodName);
logInfo.setCreateDatetime(new Date(0));
logInfo.setContent("");
logInfo.setIp(ip);
logInfo.setParams(params);
logInfo.setExceptionCode(e.getClass().getName());
logInfo.setExceptionDetail(e.getMessage());
// 保存数据库
// logService.add(log);
logger.debug("==异常通知 End==");
} catch (Exception ex) {
// 记录本地异常日志
logger.error("==异常通知异常==");
logger.error("异常信息:{}", ex.getMessage());
}
/* ==========记录本地异常日志========== */
logger.error("异常方法:{}异常代码:{}异常信息:{}参数:{}", joinPoint.getTarget()
.getClass().getName()
+ joinPoint.getSignature().getName(), e.getClass().getName(),
e.getMessage(), params);

}

/**
* 获取注解中对方法的描述信息 用于service层注解
*
* @param joinPoint
* 切点
* @return 方法描述
* @throws Exception
*/
@SuppressWarnings("rawtypes")
public static String getServiceMthodDescription(JoinPoint joinPoint)
throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String description = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description = method.getAnnotation(SystemServiceLog.class)
.description();
break;
}
}
}
return description;
}

/**
* 获取注解中对方法的描述信息 用于Controller层注解
*
* @param joinPoint
* 切点
* @return 方法描述
* @throws Exception
*/
@SuppressWarnings("rawtypes")
public static String getControllerMethodDescription(JoinPoint joinPoint)
throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String description = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description = method.getAnnotation(
SystemControllerLog.class).description();
break;
}
}
}
return description;
}
}

以上是主要的代码,spring版本是4.0,用ecplise开发,jdk1.8 (8),tomcat8. 请做过的大神解惑,谢谢
...全文
284 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,620

社区成员

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

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