标签:dwr row otf file types buffere framework warnings 没有
package cn.tj.aops;
import cn.tj.entity.Users;
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 javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
@Aspect
public class MyAspect {
@Pointcut(" execution(* cn.tj.service.UserServiceImpl.*(..))")
public void myPointCut() {
}
// 会在目标方法(login)执行之前执行
@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);
}
// 会在目标方法(login)执行之后执行
@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();
}
System.err.println(result);
return result;
}
// 目标方法执行过程中出现异常时执行,如果没有异常,此方法不执行
@AfterThrowing("myPointCut()")
public void doException() {// 异常通知
System.out.println("Exception...");
}
@Around("myPointCut()")
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);
String methodName = signature.getName();
System.err.println("方法名:" + methodName);
@SuppressWarnings("rawtypes")
Class type = signature.getDeclaringType();
System.err.println("方法属于哪个类:" + type);
int num = signature.getModifiers();
System.err.println(num);
Object obj = pjp.proceed(); // 方法继续执行,返回值为目标方法的返回值
System.out.println(obj);
return obj;
}
}
标签:dwr row otf file types buffere framework warnings 没有
原文地址:https://www.cnblogs.com/tianjin/p/13983849.html