作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个名为“LoginException”的自定义异常。它可能从任何类中抛出。所以我想建议在 throw 后做一些事情(例如,打印“Ooops”)。所以我决定使用AOP。像这样:
@Aspect
public class LogoutAdvice {
@AfterThrowing(throwing = "e")
public void myAfterThrowing(JoinPoint joinPoint, LoginException e) {
System.out.println("IDS HABBENING");
}
}
代码:
@Transactional
public DynamicTable getTable(int status_id, HttpServletRequest request)
throws HibernateException, LoginException, SQLException {
try {
ResultSet rs = requestDAO.getRequestResultSet(
cookieDAO.get(SESS_ATTR, request), status_id);
DynamicTable dt = new DynamicTable();
String[] columnArray;
LinkedList<String[]> dataList = new LinkedList<String[]>();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
columnArray = new String[columnCount - META_COLUMNS_COUNT];
for (int i = 0; i < columnArray.length; i++) {
columnArray[i] = rsmd.getColumnName(META_COLUMNS_COUNT + i + 1);
}
dt.setTitleArray(columnArray);
while (rs.next()) {
String[] dataArray = new String[columnArray.length];
for (int i = 0; i < columnArray.length; i++) {
dataArray[i] = ParamUtil.toString(rs
.getObject(META_COLUMNS_COUNT + i + 1));
}
dataList.add(dataArray);
}
dt.setDataList(dataList);
return dt;
} catch (SQLException e) {
String message = e.getMessage();
String[] errorsArray = AuthErrorsConst.ERROR;
for (int i = 0; i < errorsArray.length; i++) {
if (message.contains(errorsArray[i])) {
throw new LoginException(); // LOOK AT THIS
}
}
throw e;
}
}
我该怎么做?
最佳答案
确保异常被抛出
catch (SQLException e) {
String message = e.getMessage();
String[] errorsArray = AuthErrorsConst.ERROR;
for (int i = 0; i < errorsArray.length; i++) {
if (message.contains(errorsArray[i])) {
System.out.println("throwing LoginException")// NEW
throw new LoginException(); // LOOK AT THIS
}
}
throw e;
}
关于
@Aspect
public class LogoutAdvice {
@AfterThrowing(throwing = "e")
public void myAfterThrowing(JoinPoint joinPoint, LoginException e) {
System.out.println("IDS HABBENING");
}
}
请确定 Spring 已启用与@Aspect
一起工作,并且此外它能够扫描您的LogoutAdvice
方面,通常我如何声明它们
@Aspect
@Component// to be scanned by Spring
public class LogoutAdvice {
将您的@AfterThrowing
更改为
@AfterThrowing(pointcut = "execution(* *.*(..))",throwing = "e")
关于java - 投入AOP后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25975520/
获取字符指针并将其放入输出的简单程序。 char * get() { char ss [256]; sprintf (ss,"%d",1); return ss; } int
基本思路是这样的:我有一些 constexpr功能,我想使用 throw发出错误信号和延迟编译以避免在正常流程中出现此错误: template auto constexpr find_elt(con
我是一名优秀的程序员,十分优秀!