spring aop的案例(一)日志拦截

本文介绍Spring AOP在日志拦截中的应用,重点关注Service和Controller层面的日志记录。通过配置aplicationContext-common.xml和applicationContext-mvc.xml,结合SystemLogAspect和SystemControllerLog、SystemServiceLog注解实现日志拦截。在使用过程中,由于AOP的执行顺序,可能导致Service层异常时事务回滚,解析这一现象的原因及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

日志拦截,一般主要在service和action进行日志拦截。这里我们直接讲用法,至于原理就不做具体讲解。
我们使用spring+spring mvc架构项目

aplicationContext-common.xml:

<context:component-scan base-package="com.tonghui.thcws">
        <!--删除controller注解扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>   

<!-- 启动对@AspectJ注解的支持 -->  
<aop:aspectj-autoproxy/> 
<!--事务注解-->
<tx:annotation-driven  transaction-manager="transactionManager" proxy-target-class="true" />

applicationContext-mvc.xml:

<context:component-scan base-package="com.tonghui.thcws.web.controller">
    <!--只扫描controller注解-->    
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
</context:component-scan>

Log注解声明:SystemLogAspect.java


import java.lang.reflect.Method;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值