Aop实现日志的service层的拦截
Posted Ashan-Da宝旗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Aop实现日志的service层的拦截相关的知识,希望对你有一定的参考价值。
注意:该拦截只适用于service的实现类
1.自定义注解
package com.chinook5.util;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;
/**
* Created by Bertram on 2017/2/23.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target( ElementType.METHOD )
public @interface DBLog
String description() default "";
2.拦截方法体
package com.chinook5.util;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
/**
* Created by Bertram on 2017/2/23.
*/
@Aspect
@Component
public class LogAop
// @Resource(name="logService")
// private LogService logService;
@Pointcut("@annotation(com.chinook5.util.DBLog)")
public void log()
@After("log()")
public void afterExec(JoinPoint joinPoint) throws Exception
MethodSignature ms=(MethodSignature) joinPoint.getSignature();
Method method=ms.getMethod();
/* Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
User user = (User)session.getAttribute(Const.SESSION_USER);
*/
String m=method.getName();
String name=method.getAnnotation(DBLog.class).description();
System.out.println(m+name);
3.在spring里面配置
<aop:aspectj-autoproxy proxy-target-class="true" />
4.在service层添加注解
@Resource
private DaoSupport dao;
@DBLog(description = "添加")
public boolean insertUserInfo(String name) throws Exception
Integer count =(Integer) dao.save("UserInfoMapper.insertUserInfo",name);
if(count > 0)
return true;
return false;
以上是关于Aop实现日志的service层的拦截的主要内容,如果未能解决你的问题,请参考以下文章