您的位置:首页 > 编程语言 > Java开发

spring使用AOP做实现操作记录的功能

2019-01-21 11:03 483 查看

spring使用AOP做实现操作记录的功能

首先定义一个注解:

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface logAnnotation {

/** 要执行的操作类型比如:add操作 **/
public String operationType() default "";

/** 要执行的具体操作比如:添加用户 **/
public String operationName() default "";

}

写一个AOP类:

// 该注解标示该类为切面类
@Aspect
// 注入依赖
@Component
public class OperationLogAspect {

private HttpServletRequest request = null;

@Resource
ILogService logService;

/**
*
* @Description: 方法调用后触发   记录结束时间
* @author fei.lei
* @date 2016年11月23日 下午5:10
* @param joinPoint
*/
public  void after(JoinPoint joinPoint) {
request = getHttpServletRequest();
HttpSession session = request.getSession();
String account  = (String) session.getAttribute("account");
//获取被代理的对象
String targetName = joinPoint.getTarget().getClass().getName();
//获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息
String methodName = joinPoint.getSignature().getName();
//获取传入目标方法的参数对象
Object[] arguments = joinPoint.getArgs();
Class targetClass = null;
try {
targetClass = Class.forName(targetName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method[] methods = targetClass.getMethods();
String operationName = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs!=null&&clazzs.length == arguments.length&&method.getAnnotation(logAnnotation.class)!=null) {
operationName = method.getAnnotation(logAnnotation.class).operationName();
break;
}
}
}

Object obj =request.getParameter("fileName");
Log log = new Log();
log.setOperation(operationName);
if(account != null && operationName != ""){

log.setUserid(Integer.valueOf(account));
Date nowDate = new Date();
long dateGetTime = nowDate.getTime();
Timestamp timestamp = new Timestamp(dateGetTime);
log.setOperationtime(timestamp);
logService.insertLog(log);
}
}

/**
* @Description: 获取request
* @author fei.lei
* @date 2016年11月23日 下午5:10
* @param
* @return HttpServletRequest
*/
public HttpServletRequest getHttpServletRequest(){
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes)ra;
HttpServletRequest request = sra.getRequest();
return request;
}

}

在spring.xml中进行配置

<bean id="logAspect" class="com.honger.annotation.OperationLogAspect"></bean>

<aop:config>
<!--调用日志类-->
<aop:aspect id="LogAspect" ref="logAspect">
<!--配置在controller包下所有的类在调用之前都会被拦截-->
<aop:pointcut id="log" expression="execution(* com.honger.controller.UserController.*(..))"/>
<!-- 方法后触发 -->
<aop:after pointcut-ref="log" method="after"/>
</aop:aspect>
</aop:config>

使用

最后在想要记录的操作上加上相应的注解就可以了 /** * 修改用户 * @param user 修改后的用户信息 * @return */

@RequestMapping("/update")
@ResponseBody
@logAnnotation(operationType="修改操作",operationName="修改用户")
public ServerResult update(User user) {
//修改用户
ServerResult<Integer> serverResult = userService.updateUser(user);
UserRole userRole = new UserRole();
userRole.setUid(user.getUid());
userRole.setRid(Integer.parseInt(user.getRole()));
//根据uid修改用户对应的rid
userRoleService.update(userRole);
return serverResult;
}

作者:haveFuner
来源:CSDN
原文:https://blog.csdn.net/qq_31118837/article/details/85986494
版权声明:本文为博主原创文章,转载请附上博文链接!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐