aop拦截service&&controller《二》
来源:互联网 发布:flash游戏源码 编辑:程序博客网 时间:2024/06/05 08:03
package com.ouyeel.annotation;
import java.lang.reflect.Method;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.fastjson.JSONArray;
@Aspect
@Component
public class SystemLogAspect {
// 注入Service用于把日志保存数据库
/*
* @Resource private LogService logService;
*/
private String accessPage = "";
private long batchTime = System.currentTimeMillis();
// 本地异常日志记录对象
private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);
// Controller层切点
// @Pointcut("@annotation(com.fuliwd.annotation.SystemControllerLog)")
@Pointcut("execution(* com.ouyeel.partners.*.serviceImpl.*.*(..))")
public void serviceAspect() {
System.out.println("partnerservice切面");
}
/**
* 前置通知 用于拦截partnerservice层记录用户的操作
*
* @param joinPoint
* 切点
*/
@Before("serviceAspect()")
// @Before("serviceAspect() || serviceAspect()")
// @Before("execution(* com.fuliwd.controller..*.*(..))")
// @Before("execution(* com.fuliwd.controler.*.*(..))")
public void doBefore(JoinPoint joinPoint) {
// 定义key 路径
String urlKey = null;
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
try {
JSONArray argsArray = new JSONArray();
JSONArray typesArray = new JSONArray();
if (arguments.length > 0) {
for (Object obj : arguments) {
if (obj != null) {
argsArray.add(obj.getClass().getSimpleName());
typesArray.add(obj.getClass().getName());
}
}
}
for (Object obj : arguments) {
System.out.println(obj);
}
/* DB db = MongoManager.getDB("ouyeel_yunbang");
boolean ok =MongoManager.checkUser(db);
DBCollection col1 = db.getCollection("T_CONTROLLER_LOG_TEST");//获得要操作的集合
*/
if (targetName != null) {
System.out.println("==========执行partnerservice前置通知===============");
System.out.println("service");
/*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();*/
/*Map<String,Object> serviceInsertMap = new HashMap<String,Object>();
System.out.println(methodName + "====" + JSONObject.toJSONString(arguments[0]));
serviceInsertMap.put("service", JSONObject.toJSONString(arguments[0]));*/
/*BasicDBObject filter_dbobject = new BasicDBObject();
//获取controller中存入的批次时间的值,以此值为依据进行查询已有日志
filter_dbobject.put("batchTime", batchTime);
DBCursor cursor = col1.find(filter_dbobject);
List<DBObject> list = cursor.toArray();
if(list != null && list.size() > 0){
//获取查询结果的第一行,因为同一批次时间只会有一条记录
DBObject dbFind = list.get(0);
System.out.println("==========================" + dbFind + "==========================");
//将查询结果转换为json字符串
String result = JSON.serialize(dbFind);
System.out.println("==========================" + result + "==========================");
//讲json字符串转换为JSON对象
JSONObject jsonObject = JSONObject.parseObject(result);
System.out.println("==========================" + jsonObject + "==========================");
//根据转换出来的json对象获取对应url_+batchTime的key对应的值
Object obj = jsonObject.get(accessPage);
System.out.println("==========================" + obj.toString() + "==========================");
//将url_+batchTime的key对应的值加入service的信息
JSONObject jsonResult = JSONObject.parseObject(obj.toString());
jsonResult.put("service", JSONObject.toJSONString(arguments[0]));
//将修改后的JSON对象放入以url_+batchTime为key的map中,并对mongodb做修改操作,同时把执勤controller生成的批次时间予以覆盖,达到所需效果
Map<String,Object> serviceInsertMap = new HashMap<String,Object>();
serviceInsertMap.put(accessPage, jsonResult);
DBObject newDBO = (DBObject) JSON.parse(JSONObject.toJSONString(serviceInsertMap));
col1.update(dbFind,newDBO);*/
}
} catch (Exception e) {
e.printStackTrace();
}
if (logger.isInfoEnabled()) {
logger.info("before " + joinPoint);
}
}
}
- aop拦截service&&controller《二》
- aop拦截service&&controller《一》
- 日志AOP拦截Controller
- spring Aop拦截controller方法
- SpringMVC的AOP拦截controller
- spring aop学习--拦截controller
- aop:aspectj-autoproxy, SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- spring aop 拦截spring mvc controller
- 在springMVC中使用AOP拦截controller
- SpringMVC关于AOP拦截controller的注意事项
- spring4.x aop拦截spring mvc controller
- spring aop拦截controller层失效
- spring aop拦截Controller做参数校验
- Spring AOP无法拦截Controller中的方法
- Spring Aop拦截controller配置注意事项
- SpringMVC关于AOP拦截controller的注意事项
- Spring Aop自定义注解拦截Controller实现日志管理
- Springmvc AOP拦截Controller获取session(自定义注解的方式)
- 运算符重载 编程题#3(专项课程3;重载:* operator, & operator=, & operator(); 函数 memcpy 的使用)
- 第五周项目1—建立顺序栈算法库
- 数组序列转换2
- CMake入门教程
- mysql 数据表 查找 where and or
- aop拦截service&&controller《二》
- 重定向和权限设置常见题目
- nginx相关(静态资源、代理服务、负载均衡、动态缓存)
- HTML DOM方法
- 第14篇
- < 笔记 > DOM
- js中onclick事件里面传递参数
- linux各发行版之间的区别跟联系
- 百度6.0的获取权限