spring AOP 通知
2021-03-17 03:25
标签:dwr row otf file types buffere framework warnings 没有 package cn.tj.aops; import cn.tj.entity.Users; import javax.servlet.http.HttpServletRequest; @Pointcut(" execution(* cn.tj.service.UserServiceImpl.*(..))") // 会在目标方法(login)执行之前执行 // 会在目标方法(login)执行之后执行 // 目标方法执行过程中没有出现异常时执行,如果又异常,此方法不执行 System.err.println(result); // 目标方法执行过程中出现异常时执行,如果没有异常,此方法不执行 @Around("myPointCut()") String methodName = signature.getName(); int num = signature.getModifiers(); return obj; spring AOP 通知 标签:dwr row otf file types buffere framework warnings 没有 原文地址:https://www.cnblogs.com/tianjin/p/13983849.html
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
@Aspect
public class MyAspect {
public void myPointCut() {
}
@Before("myPointCut()")
public void doBefore(JoinPoint jp) {// 通过此形参可以获取目标方法中的参数
System.out.println("before...");
// Object[] args = jp.getArgs();//通过此形参可以获取目标方法中的所有参数
HttpServletRequest req = (HttpServletRequest) jp.getArgs()[1];
String IP = req.getRemoteAddr();
System.out.println("在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "登陆了系统,IP为:" + IP);
}
@After("myPointCut()")
public void doAfter(JoinPoint jp) {// 通过此形参可以获取目标方法中的参数
System.out.println("after...");
HttpServletRequest req = (HttpServletRequest) jp.getArgs()[1];
String IP = req.getRemoteAddr();
try {
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(new File("C:/Users/Administrator/Desktop/log.txt"))));
String log = "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "登陆了系统,IP为:" + IP;
bw.write("after");
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterReturning(pointcut = "myPointCut()", returning = "result")
public Object doReturn(JoinPoint jp, Object result) {
System.out.println("return...");
try {
HttpServletRequest req = (HttpServletRequest) jp.getArgs()[1];
String IP = req.getRemoteAddr();
Users user = (Users) result;
System.out.println("user:" + user);
File file = new File("C:/Users/Administrator/Desktop/log.txt");
// boolean flag = file.exists();
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
String log = "aaa" + "在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "登陆了系统,IP为:"
+ IP;
// bw.write(log);// 此方法会刷新文件内容
bw.append(log);
bw.newLine();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@AfterThrowing("myPointCut()")
public void doException() {// 异常通知
System.out.println("Exception...");
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {// 环绕通知
System.out.println("around...");
Object[] args = pjp.getArgs();
for (Object obj : args) {
System.err.println(obj);
}
Signature signature = pjp.getSignature();
System.err.println("方法签名为:" + signature);
System.err.println("方法名:" + methodName);
@SuppressWarnings("rawtypes")
Class type = signature.getDeclaringType();
System.err.println("方法属于哪个类:" + type);
System.err.println(num);
Object obj = pjp.proceed(); // 方法继续执行,返回值为目标方法的返回值
System.out.println(obj);
}
}