Spring Aop 日志操作

需求是這樣的

在已經(jīng)寫好的系統(tǒng)中添加管理員的操作記錄。并且總管理權(quán)限可以查看這些記錄。包括操作的時(shí)間 內(nèi)容和操作的結(jié)果以及IP地址。

查找各方面資料后。感覺最適合我們項(xiàng)目的就是Spring Aop 做切面操作。

操作過程很簡單。

首先在 Spring的配置文件中 applicationContext.xml 添加對aop的掃描并打開自動(dòng)代理

    <!-- 配置aop -->
    <context:component-scan base-package="com.onepay.aop"></context:component-scan>
    <!-- 激活自動(dòng)代理 -->
    <aop:aspectj-autoproxy proxy-target-class="true" ></aop:aspectj-autoproxy>

在web.xml中添加對對webcontext的監(jiān)聽 保證隨時(shí)可以取到request和response

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

之后就可以寫切面 攔截service的請求

/**
 * 登錄操作切面
 * @author dell
 *
 */
@Component
@Aspect
public class LoginAspect {
    
    @Resource
    private HttpServletRequest request;
    
    //配置切點(diǎn)
    @Pointcut("execution(* com.onepay.service.WYUserService.*(..))")
    public void point(){    }
    /*
     * 配置前置通知,使用在方法aspect()上注冊的切入點(diǎn)
     * 同時(shí)接受JoinPoint切入點(diǎn)對象,可以沒有該參數(shù)
     */
    @Before("point()")
    public void before(JoinPoint  joinPoint){
        System.out.println("------------------------");
    }
    
    //配置后置通知,使用在方法aspect()上注冊的切入點(diǎn)
    @After("point()")
    public void after(JoinPoint joinPoint) throws Throwable{
    }
    
    //配置環(huán)繞通知,使用在方法aspect()上注冊的切入點(diǎn)
    @Around("point()")
    public Object around(ProceedingJoinPoint joinPoint)throws Throwable{
        Object returnVal = joinPoint.proceed();
        System.out.println("around 目標(biāo)方法名為:" + joinPoint.getSignature().getName());
        System.out.println("around 目標(biāo)方法所屬類的簡單類名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());
        System.out.println("around 目標(biāo)方法所屬類的類名:" + joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("around 目標(biāo)方法聲明類型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println("around 第" + (i + 1) + "個(gè)參數(shù)為:" + args[i]);
        }
        System.out.println("around 返回值:"+returnVal);
                //這里必須返回returnVal 否則controller層將得不到反饋。并且這個(gè)returnVal可以在這里修改會(huì)再返回到controller層。
        return returnVal;
    }
    
    //配置后置返回通知,使用在方法aspect()上注冊的切入點(diǎn)
    @AfterReturning("point()")
    public void afterReturn(JoinPoint joinPoint)throws Throwable{
        System.out.println("------------------------");
    }
    
    //配置拋出異常后通知,使用在方法aspect()上注冊的切入點(diǎn)
    @AfterThrowing(pointcut="point()", throwing="ex")
    public void afterThrow(JoinPoint joinPoint, Exception ex){
            System.out.println("afterThrow " + joinPoint + "\t" + ex.getMessage());
    }
    
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容