gpt4 book ai didi

java - 投入AOP后

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:46 24 4
gpt4 key购买 nike

我有一个名为“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com