1.在lib目录下添加@aspect的jar包
2.自定义注解类
3.spring controller记录
@Aspect
@Component
public class SystemLogAspect {
@Resource
private UsractionlogService usractionlogService;
@Resource
private UsractionlogenService usractionlogenService;
@Resource
private HttpServletRequest request;
private final static int LOG_NORMAL=1;
private final static int LOG_UNNORMAL=0;
public SystemLogAspect(){
System.out.println("SystemLogAspect is initing!!!!");
}
public Logger logger = Logger.getLogger(SystemLogAspect.class);
//声明AOP切入点,凡是使用了controllerAspect的方法均被拦截
@Pointcut("@annotation(com.ams.aspect.MethodLog)")
public void controllerAspect() {
System.out.println("ddddd");
}
```@After("controllerAspect()")
public void doAfter(JoinPoint joinPoint){
String loca = (String)request.getSession().getAttribute("location");
//System.out.println("我是否被拦截过!!!");
HttpSession session = request.getSession();
//读取session中的用户
TableUsers user = (TableUsers) session.getAttribute("tableusers");
// 获取请求ip
String ip = Tools.getRemoteHost(request);
String realPath = request.getSession().getServletContext().getRealPath("vod/ipdate/");
try {
if(!loca.equals("en")){
Usractionlog myLog = new Usractionlog();
myLog.setUserIp(ip);
myLog.setUsrActionflag(joinPoint.getSignature().getName());
Date day=new Date();
myLog.setUsrActiontime(day);
String comand=getControllerMethodDescription(joinPoint);
String canshu=getCanshuDescription(joinPoint);
if(user!=null){
myLog.setUsrName(user.getUsername());
if(comand==""){
myLog.setUsrAction(comand);
}else{
myLog.setUsrAction(comand);
}
}else {
myLog.setUsrName("匿名用户");
if(comand==""){
myLog.setUsrAction(comand);
}else{
myLog.setUsrAction(comand);
}
}
if(comand!=null){
usractionlogService.save(myLog);
}
}else{
UsractionlogEn usrActionlog = new UsractionlogEn();
usrActionlog.setUserIp(ip);
usrActionlog.setUsrActionflag(joinPoint.getSignature().getName());
Date day=new Date();
usrActionlog.setUsrActiontime(day);
String comand=getControllerMethodDescription(joinPoint);
String canshu=getCanshuDescription(joinPoint);
if(user!=null){
usrActionlog.setUsrName(user.getUsername());
if(comand==""){
usrActionlog.setUsrAction(comand);
}else{
usrActionlog.setUsrAction(comand);
}
}else {
usrActionlog.setUsrName("匿名用户");
if(comand==""){
usrActionlog.setUsrAction(comand);
}else{
usrActionlog.setUsrAction(comand);
}
}
if(comand!=null){
usractionlogenService.save(usrActionlog);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取注解中对方法的描述信息 用于Controller层注解
*
* @param joinPoint 切点
* @return 方法描述
* @throws Exception
*/
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 = "";
String canshu="";
for (Method method : methods) {
if(method!=null){
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes(); //对比方法中参数的个数
if (clazzs.length == arguments.length) {
description = method.getAnnotation(MethodLog. class).remark();
System.out.println("description:"+description);
for (int i = 0; i < arguments .length; i++) {
if(arguments[i]!=null){
canshu= arguments[i].toString();
System.out.println(" arguments[i].toString()"+ arguments[i].toString());
}
}
break;
}
}
}
}
return description;
}
/**
*获取aop切点方法的参数
*/
public static String getCanshuDescription(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 = "";
String canshu="";
for (Method method : methods) {
if(method!=null){
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes(); //对比方法中参数的个数
if (clazzs.length == arguments.length) {
description = method.getAnnotation(MethodLog. class).remark();
System.out.println("description:"+description);
for (int i = 0; i < arguments .length; i++) {
if(arguments[i]!=null){
canshu= arguments[i].toString();
System.out.println(" arguments[i].toString()"+ arguments[i].toString());
}
}
break;
}
}
}
}
return canshu;
}
}
4.在springmvc配置文件
context:annotation-config</context:annotation-config>
<context:component-scan base-package=“com.ams.action”></context:component-scan>
<aop:aspectj-autoproxy proxy-target-class=“true”/>
引入上面的controller层
5,在controller层方面里面添加自定义的注解@MethodLog
@RequestMapping("/list")
@MethodLog(remark="获取公司信息") 用户执行后生成一条记录
public String list(Model model){
List<Company> companylist = companyService.findAll();
if(companylist!=null&&companylist.size()>0){
Company company = companylist.get(0);
model.addAttribute("company",company);
}
return "/admin/manage/company/company.ftl";
}
6,效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200422182925225.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDk3NTMyMg==,size_16,color_FFFFFF,t_70#pic_center)